Programmatically adding a route in Play2.0

浪子不回头ぞ 提交于 2020-01-01 09:17:27

问题


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?


回答1:


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




回答2:


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




回答3:


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)

GET     /:page  controllers.Application.showPage(page)

Put your logic which you want to execute on runtime in controller class

public static Result showPage(String page){

    if(page.contains("abc"){
      .....
    } else {
      //return 404
    }
}

I'm not sure if it would fit into your requirement but in most of the scenarios it will suffice.




回答4:


*play2.0* *Add this line in your routes file* GET /somePath controller.methodName()



来源:https://stackoverflow.com/questions/10116286/programmatically-adding-a-route-in-play2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!