How to get list of all Route URL strings in play framework?
I obtained the desired list by using the documentation
method provided by Router trait. The documentation method returns Seq[(String, String, String)]
.Here each tuple has the format:
( {http-method} , {url} , {controller method} )
The Router trait is extended by all the autogenerated Routes.scala
classes. Scala-compiler generated a separate Routes.scala
for each routes
file in the application. These auto-generated Routes.scala
files implement all the methods of Router trait including the documentation method that we discussed above.
So,to get list of all URLs, I simply had to inject the Router trait
and then access the documentation method
:
import play.api.routing.Router
class MyClass @Inject()(router: Router) {
def getAllURLs:Seq[String] = router.documentation.map(k => k._2)
}
Update for Play 2.7, Scala:
class MyController @Inject()(routesProvider: Provider[play.api.routing.Router]) {
lazy val routes: Seq[(String, String, String)] = routesProvider.get.documentation
}
from discussion for play 2.6