[英]URL routing - regex match with trailing slash and without
I am using Spiffy-Route library to route my URLs. 我正在使用Spiffy-Route库来路由我的URL。 Everything is great, able to freely route the needed urls, but I am not really able to add a wildcard, as it's not included in the api.
一切都很好,能够自由地路由所需的url,但是我真的不能添加通配符,因为它不包含在api中。
My route looks like this: 我的路线如下所示:
/{id:\d+}-{string}
Means I can route to it like this: 意味着我可以这样路由:
.com/5-helloworld
But when I route to it like this: 但是当我这样路由时:
.com/5-helloworld/
It will not find a match, because of the slash. 由于斜杠,它将找不到匹配项。 I have tried doing
/{id}-{string}/?
我尝试做
/{id}-{string}/?
but the parameter {string} will include all the slashes after it inside it's value. 但参数{string}将在其值内包含所有斜杠。
I have been going through some Regex questions over stackoverflow, to find out how to make the pattern support with slash and without - the common answer was ([^/]+)
, but this piece of regex doesn't really affect at all in my case. 我一直在通过stackoverflow进行一些正则表达式问题,以找出如何使用斜杠和不使用斜杠来支持模式-常见的答案是
([^/]+)
,但是这条正则表达式根本不会真正影响我的情况。
How can I do this with Spiffy router? 如何使用Spiffy路由器执行此操作?
If I do something like /{id}-{string}([^/]+)/?
如果我做类似
/{id}-{string}([^/]+)/?
事情/{id}-{string}([^/]+)/?
and route to it like this: /5-hey/
并按如下方式路由到它:
/5-hey/
The output will be 5he
instead of 5hey
according to this echo: 根据此回
5hey
,输出将为5he
而不是5hey
:
echo parent::getRouteMatch()->get('id') . parent::getRouteMatch()->get('string');
Will I have to modify the API to get this to work? 我是否需要修改API才能使其正常工作?
[^/]
means anything but a forward slash. [^/]
表示除正斜杠之外的任何内容。 How about this: 这个怎么样:
/{id:\d+}-{string:[^/]+}\/?
I should note I also don't know Spiffy. 我应该注意我也不了解Spiffy。 Regex-wise that should work though.
正则表达式应该可以工作。
If you want to allow strings after the slash, use .*
to allow and discard, or repeat {string:[^/]+
to capture (substitute string to another name in that case though) 如果要在斜杠后允许使用字符串,请使用
.*
允许并丢弃,或重复使用{string:[^/]+
进行捕获(尽管在这种情况下,将字符串替换为另一个名称)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.