我想做这样的事情:
/**
* @Route("^/secured") <-- this would not work, just an example
*/
public function securedAction(){
//return secured JS frontend
}
并让 symfony 将任何路由 (.com/secured/something
; .com/secured/anything/else
) 匹配到这个 Action ,而无需手动定义所有路由.
symfony 支持吗?我想不出搜索这个的条件。
如何在不基于第一个节点 (/secured
) 手动定义所有路由的情况下匹配并路由到此 Controller 操作?
最佳答案
/**
* @Route("/secured/{anything}", name="_secured", defaults={"anything" = null}, requirements={"anything"=".+"})
*/
public function securedAction($anything){
//return secured JS frontend
}
name
- 只是路线的名称。
defaults
- 如果您没有在 url 中提供参数,您可以在这里设置参数的默认值:/secured/
requirements
- 参数要求,在这种情况下 anything
可以包含正斜杠:http://symfony.com/doc/current/cookbook/routing/slash_in_parameter.html ,但您必须自己在 Controller 操作中处理它:
例如,如果您提供 url:/secured/anything/another_thing/one_more_thing
可以通过explode('/', $anything);
获取所有参数
结果将是:
array:3 [
0 => "anything"
1 => "another_thing"
2 => "one_more_thing" ]
/secured/
之后的所有内容都是一个参数 $anything
。
关于php - Symfony 路由 : match anything after first node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32575962/