playframework 2 scala 中 play.mvc.Router.routes 的等价物是什么?
在 playframework 1.x 中,我可以迭代 Controller 中的可用路由:
for(Route route:Router.routes){
...
}
我将如何使用 playframework 2 scala 做到这一点?
未找到 dev mode template似乎能够迭代它们,但我需要在 Controller 中执行此操作。
最佳答案
Play 2.0 无法做到这一点。
Routes被定义为一个PartialFunction[RequestHeader, Handler]
,没有办法知道这个函数的域。
尽管如此,如 not found 模板中所示,您可以检索有关应用程序路由的一些信息:Play 2.0 路由器为每个路由生成一个 documentation
字段返回应用程序、它的 HTTP 方法(GET
、PUT
等)、它的路径模式以及最后在 conf/routes< 中编写的调用
文件。
路由器会生成一个 Routes
对象,该对象在您的 Play 应用程序启动时加载,您可以通过以下方式访问它:
for {
routes <- play.api.Play.current.routes.toList
(method, pattern, call) <- routes.documentation
} yield {
…
}
关于scala - 如何迭代 playframework 2 scala 应用程序中的路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10538455/