Play框架:项目中的多个路由文件

7

在Play项目中是否可以拥有多个conf/routes文件?例如:

   -> conf/ 
           routes
           utils.routes
           user.routes

还是有解决办法的吧?我理解conf/routes会被编译并验证等,应该可以通过某种方式覆盖这个逻辑。

2个回答

10

你可以尝试按照模块化的方法进行拆分,具体方式请参考官方文档

你也可以把路由文件分成多个小片段。如果你想创建一个强大、可重用的多模块Play应用程序,这将是非常有用的功能。

简而言之,您可以将应用程序代码分组到一个或多个模块中,每个模块都有自己的路由文件。然后,您可以像以下示例中那样将较小的路由文件包含到全局路由文件中:

conf/routes:

GET /index                  controllers.HomeController.index()

->  /admin admin.Routes

GET     /assets/*file       controllers.Assets.at(path="/public", file)

模块/管理/配置/管理.路由:

GET /index                  controllers.admin.HomeController.index()

GET /assets/*file           controllers.Assets.at(path="/public/lib/myadmin", file)

完美运作,正是我所寻找的!谢谢。 - Pavel

1

基于javierhe的回答,如果你正在使用单项目和DI,但仍想使用多个路由文件,可以按如下方式操作。无需使用sbt多项目设置。

conf/ 
     routes
     admin.routes

conf/routes:

GET /index                  controllers.HomeController.index()

->  /admin admin.Routes

conf/admin.routes:

GET /index                  controllers.admin.HomeController.index()

在应用程序加载器中,将以下内容添加到构建路由中。
val adminRouter: admin.Routes = {
        val prefix = "/"
        wire[admin.Routes] //replace it with constructor if you do manual DI
}
val router: Routes = {
        val prefix = "/"
        wire[Routes] //replace it with constructor if you do manual DI
}

测试过使用 play 2.8.x 和 macwire。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接