【问题标题】:Symfony routing: match anything after first nodeSymfony 路由:匹配第一个节点之后的任何内容
【发布时间】:2015-12-11 02:43:34
【问题描述】:

我想做这样的事情:

/**
 * @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) 在不手动定义所有路由的情况下匹配并路由到此控制器操作?

【问题讨论】:

    标签: php symfony routing


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

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

    你可以通过explode('/', $anything);获取所有参数

    结果将是:

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

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

    【讨论】:

    • 我要试试这个,你能解释一下这里发生了什么吗?
    • 漂亮!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 2020-01-29
    • 2015-08-05
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多