Currently I define my app's javascript router in a fairly verbose way
def javascriptRoutes = Action { implicit request =>
import routes.javascript._
Ok(Routes.javascriptRouter("jsRoutes")(
Login.method1,Login.Method2,
OtherController.method1,OtherController.method2,
//[...]
)).as("text/javascript")
}
What I really would like to do is to create a javascriptRouter
with all of the routes in the routes
file, so I don't have to manually update the javascriptRoutes
definition each time I add a new controller method.
Is there a way to accomplish this task, or is there even a slightly less verbose way of defining the javascriptRouter
?
Very nice solution by thatsmydoing. If you have the JavaScript-routes under some other subpackage, you need to declare routeCache like this
You can do it via reflection like so:
This was derived from the generated source files found in target/scala-2.x.x/src_managed. You could actually add your own source generator and parse the routes file yourself, but I find doing it via reflection easier.
An additional thing you might want to do is filter out the methods you don't want as this will give you ALL the routes (including the javascriptRouter itself).
Expanding @rochb's answer for Play 2.4 Java, where the package names are slightly different, with support for multiple controller packages.
Also, if you are using Play 2.4, some Classes/packages have been changed:
I needed that in java. Copying it here in case it is of use for someone.