Symfony routing: match anything after first node

后端 未结 1 1137
逝去的感伤
逝去的感伤 2020-12-18 12:50

I\'d like to do something like this:

/**
 * @Route(\"^/secured\") <-- this would not work, just an example
 */
public function securedAction(){
  //retur         


        
1条回答
  •  悲&欢浪女
    2020-12-18 13:10

    /**
     * @Route("/secured/{anything}", name="_secured", defaults={"anything" = null}, requirements={"anything"=".+"})
     */
    
    public function securedAction($anything){
        //return secured JS frontend 
    }
    

    name - just name of the route.

    defaults - here you can set default value of the parameter, if you not provide parameter in the url: /secured/

    requirements - requirements for parameter(s), in this case anything can contain forward slash: http://symfony.com/doc/current/cookbook/routing/slash_in_parameter.html , but you must handle it in your controller action yourself:

    for example if you provide url: /secured/anything/another_thing/one_more_thing

    you can get all parameters by explode('/', $anything);

    and the results will be:

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

    Just everything after /secured/ will be one parameter $anything.

    0 讨论(0)
提交回复
热议问题