gpt4 book ai didi

scala - 忽略路由中的参数

转载 作者:行者123 更新时间:2023-12-04 18:06:32 24 4
gpt4 key购买 nike

Play 的优点之一是它不会规定您的 URL 格式。这很好,因为我正在移植一个应用程序并且需要保留与旧 URL 的向后兼容性。
我想匹配所有以 /foo/bar 开头的 URL :

/foo/bar
/foo/bar/0
/foo/bar/1
/foo/bar/baz
我该怎么做呢?
我找不到太多文档。我找到了 1.2.7 的旧文档那说

You can tell Play that you want to match both URLs by adding a question mark after the trailing slash. For example:

GET /clients/? Clients.index

The URI pattern cannot have any optional part except for that trailing slash.


作为它自己的特例,这有点有趣。 IDK 多少仍然是正确的,因为它不在当前文档中。

我试过
GET     /foo/bar             Foo.bar
GET /foo/bar/*unused Foo.bar
GET     /foo/bar             Foo.bar
GET /foo/bar/$unused<.*> Foo.bar
但编译失败。

Compilation error[Missing parameter in call definition: unused]



最后,我尝试重新定义 Foo.bar接受一个垃圾参数(默认为空字符串)。
GET     /foo/bar             Foo.bar
GET /foo/bar/*unused Foo.bar(unused)
GET     /foo/bar             Foo.bar
GET /foo/bar/$unused<.*> Foo.bar(unused)
但它仍然没有用。

conflicting symbols both originated in file '/home/paul/my-play-project/target/scala-2.10/src_managed/main/routes_reverseRouting.scala'



如何匹配 URL 前缀或忽略参数?

最佳答案

解决方案归功于@wingedsubmariner

GET     /foo/bar             Foo.bar
GET /foo/bar/$unused<.*> Foo.barIgnoreUnused(unused)


object Foo {

def bar = Ok("")

def barIgnoreUnused(unused: String) = bar

}

令人惊讶的是,这似乎不起作用。
GET     /foo/bar$unused<.*> Foo.bar(unused)

Play 做的正确的事情可能是允许未使用的参数,但这个(无可否认的丑陋)解决方案有效。

关于scala - 忽略路由中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25020415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com