scala - 为一堆 url 添加前缀

标签 scala playframework routing playframework-2.2

我有一堆具有相同前缀的路由:

# with prefixes
GET         /api/v1.0/action1   controllers.Api.action1
GET         /api/v1.0/action2   controllers.Api.action2
GET         /api/v1.0/action3   controllers.Api.action3
GET         /api/v1.0/action4   controllers.Api.action4

# normal urls
GET         /action1   controllers.Home.action1
GET         /action2   controllers.Home.action2

我想摆脱 /api/v1.0/ 的重复。 url 必须保持不变,我只是不想为 route 文件中的每个 url 手动编写它们。在 Rails 中这是可能的。有什么办法吗?

最佳答案

要么您为这些操作实现自己的路由器,遵循 James Ropers' post ,正如 Rich 所提到的。这样做,允许您将以下内容添加到您的路由文件中:

->  /api/v1.0   YourPathBindableController   

或者,您可以使用插件,例如 navigator ,它为您提供高级路由。您的导航器路线文件将包含类似以下内容:

   // Namespace ...
namespace("api"){
  namespace("v1"){
    GET on "index" to Application.index _
  }
}

// ... or with reverse routing support
val api = new Namespace("api"){
  val v2 = new Namespace("v2"){
    val about = GET on "about" to Application.about _
  }
}

关于scala - 为一堆 url 添加前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21519426/

相关文章:

Scala - 定义自己的中缀运算符

scala - Play 2.7 中的 RequestScoped

c# - 如何使用 OpenStreetMap 获取两点之间的距离

scala - 如何将过滤器代码提取到局部变量

ScalaTest 规范编译错误

Scala 并行集合 - 如何提前返回?

scala - Play Framework 2.2.0 [scala] - WebSocket.async 与 WebSocket.using[T]

java - 使用 Activator 在 Play Framework 测试中传递系统属性

php - 路由路径中的 Symfony2 特殊字符

ruby-on-rails - 如何在 Rails 4 中为 AngularJS 更正设置根路由?