Programmatically adding a route in Play2.0

playframework-2.0

Solution

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:

override def onRouteRequest(request: RequestHeader): Option[Handler] = {
  //do our own path matching first - otherwise pass it onto play.
  request.path match {
    case "/injectedRoute" => Some(controllers.Application.customRoute)
    case _ => Play.maybeApplication.flatMap(_.routes.flatMap {
      router =>
      router.handlerFor(request)
    })
  }
}

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

Problem

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?

Original source