在Play2.0中以编程方式添加路线 [英] Programmatically adding a route in Play2.0
问题描述
在游戏1.2.X中我们可以做到
In play 1.2.X we could do
Router.addRoute("GET", "/somePath", "controller.methodName");
我正在编写一个添加路由"的模块,该路由将由模块中的控制器处理.这是一个OAuth处理程序,希望使用户能够轻松处理OAuth握手等.
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.
如何在Play 2.0中做到这一点?
How can I do this in Play 2.0?
推荐答案
您不能以编程方式将其添加到Routes对象,但是可以拦截Web请求并通过覆盖GlobalSettings.onRouteRequest
自己处理它们.例如:
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)
})
}
}
我不知道这是否是推荐的方法,但是它对我有用.这是github上的示例: https://github.com/edeustace/play-injected-routes -示例
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中以编程方式添加路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!