Symfony2:带有斜杠和可选参数的URL [英] Symfony2: URLs with trailing slash and an optional parameter
问题描述
我希望我的应用程序中的所有URL都带有斜杠。我的route.yml中有以下路线:
I want all URLs in my application to have a trailing slash. I have the following route in my route.yml:
foo_route:
pattern: /foo/{page}/
defaults: { _controller: FooBundle:Foo:list, page: 1 }
requirements:
page: \d+
对'/ foo / 1 /'的请求可以正常工作,但是由于URL模式中的斜杠,对'/ foo /'的请求不匹配。
Requests to '/foo/1/' work fine, however requests to '/foo/' are not matched because of the trailing slash in the URL pattern.
如何定义带有斜杠和可选参数的路由?我知道我可以为这两种情况定义2条不同的路线,但是我想避免这种情况。
How can I define routes with trailing slashes and an optional parameter? I am aware I can define 2 different routes for the two cases, but I want to avoid that.
推荐答案
相关案例,您可以匹配以下三种模式:
In a separate but related case, you can match the following three patterns:
/foo
/foo/
/foo/page/1
通过消除路径中的尾部反斜杠并放宽正则表达式以匹配0或更多字符(*),而不是1个或多个(+):
by eliminating the trailing backslash in the route and relaxing the regular expression to match 0 or more characters (*), instead of 1 or more (+):
foo_route:
pattern: /foo/{page}
defaults: { _controller: FooBundle:Foo:list, page: 1 }
requirements:
page: \d*
但是与/ foo / page / 1 /
However that will not match /foo/page/1/
这篇关于Symfony2:带有斜杠和可选参数的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!