In play 1.2.X we could do
Router.addRoute("GET", "/somePath", "controller.methodName");
I'm writing a module that adds a "route" that will be handled by a controller in the module. It's a OAuth handler and want to make it easy for users to not have to deal with the OAuth handshake etc.
How can I do this in Play 2.0?
I am not sure you can.
The concept of Play 2.0 was to focus on type safety, which includes the routes file. The routes file is now compiled, rather than interpreted at run time. If you look at the code for the routes file, it generates a scala class from the routes file itself. Therefore, runtime manipulation would simply be ignored.
Unfortunately it looks as though your routes must be defined in the routes file, unless you are willing to intercept the http requests to check for specific routes yourself, which is what the /@documentation links appear to do in the ApplicationProvider scala class.
Also see this bug post https://play.lighthouseapp.com/projects/82401/tickets/12-support-multiple-routes-file-and-inclusion
You can't add programmatically to the Routes object, but you can intercept web requests and handle them yourself by overriding
GlobalSettings.onRouteRequest
. For example:I've no idea if this is the recommended approach, but it works for me. Here's a sample on github: https://github.com/edeustace/play-injected-routes-example
*play2.0* *Add this line in your routes file* GET /somePath controller.methodName()
You can add a generic route in your routes file (at the end of file as its priority would be assessed on the basis of its location of declaration)
Put your logic which you want to execute on runtime in controller class
I'm not sure if it would fit into your requirement but in most of the scenarios it will suffice.