Symfony 重定向所有以 url 结尾的路由
Symfony redirect all routes ending with on url
在我的 Symfony 项目中,假设我们有以下路线:
$route= new Route('/my-method',array(
'_controller'=>'AppBundle:MyController:myMethod'
));
$collection->add('my_method_route',$route);
$route= new Route('/my-method2',array(
'_controller'=>'AppBundle:MyController:myAnotherMethod'
));
$collection->add('my_method2_route',$route);
$route= new Route('/anotherStuff',array(
'_controller'=>'AppBundle:MyController:anotherStuff'
));
$collection->add('another_stuff',$route);
$route= new Route('/anotherStuff/{param}',array(
'_controller'=>'AppBundle:MyController:anotherStuffOneParam'
));
$collection->add('another_stuff',$route);
$route= new Route('/anotherStuff/{param}/{another_param}',array(
'_controller'=>'AppBundle:MyController:anotherStuffTwoParam'
));
$collection->add('another_stuff',$route);
我想要 url 以 /
结尾,例如。 http//example.com/my-method/
重定向到 http//example.com/my-method
.
所以我在考虑是否可以使用正则表达式作为路由的第一个参数,或者配置 Symfony 的路由系统 showhow 以执行重定向。
但是 Symfony 可以接受正则表达式或者 url 上的可选部分吗?例如,使用 '/my-method2/?'
来表明路线可能以 /
结尾,而忽略这些内容。
您想将所有以尾部斜杠结尾的 url 重定向到相同的 URL 而不正确吗?
您必须创建一个匹配 .*/$ 的路由,然后在控制器中删除这个“/”。
并且有一篇文章在 symfony 文档中给出了代码:
https://symfony.com/doc/current/routing/redirect_trailing_slash.html
在我的 Symfony 项目中,假设我们有以下路线:
$route= new Route('/my-method',array(
'_controller'=>'AppBundle:MyController:myMethod'
));
$collection->add('my_method_route',$route);
$route= new Route('/my-method2',array(
'_controller'=>'AppBundle:MyController:myAnotherMethod'
));
$collection->add('my_method2_route',$route);
$route= new Route('/anotherStuff',array(
'_controller'=>'AppBundle:MyController:anotherStuff'
));
$collection->add('another_stuff',$route);
$route= new Route('/anotherStuff/{param}',array(
'_controller'=>'AppBundle:MyController:anotherStuffOneParam'
));
$collection->add('another_stuff',$route);
$route= new Route('/anotherStuff/{param}/{another_param}',array(
'_controller'=>'AppBundle:MyController:anotherStuffTwoParam'
));
$collection->add('another_stuff',$route);
我想要 url 以 /
结尾,例如。 http//example.com/my-method/
重定向到 http//example.com/my-method
.
所以我在考虑是否可以使用正则表达式作为路由的第一个参数,或者配置 Symfony 的路由系统 showhow 以执行重定向。
但是 Symfony 可以接受正则表达式或者 url 上的可选部分吗?例如,使用 '/my-method2/?'
来表明路线可能以 /
结尾,而忽略这些内容。
您想将所有以尾部斜杠结尾的 url 重定向到相同的 URL 而不正确吗?
您必须创建一个匹配 .*/$ 的路由,然后在控制器中删除这个“/”。
并且有一篇文章在 symfony 文档中给出了代码: https://symfony.com/doc/current/routing/redirect_trailing_slash.html