Symfony 路由:匹配第一个节点之后的任何内容

Symfony routing: match anything after first node

我想做这样的事情:

/**
 * @Route("^/secured") <-- this would not work, just an example
 */
public function securedAction(){
  //return secured JS frontend 
}

并让 symfony 将任何路由(.com/secured/something.com/secured/anything/else)匹配到这一操作,而无需手动定义所有路由。

symfony 支持吗?我想不出搜索这个的条件。

如何在不根据第一个节点 (/secured) 手动定义所有路由的情况下匹配并路由到此控制器操作?

/**
 * @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 ,但您必须自己在控制器操作中处理它:

例如,如果您提供 url:/secured/anything/another_thing/one_more_thing

可以通过explode('/', $anything);

获取所有参数

结果将是:

array:3 [
  0 => "anything"
  1 => "another_thing"
  2 => "one_more_thing" ]

/secured/ 之后的所有内容都是一个参数 $anything