Symfony routing: match anything after first node

末鹿安然 提交于 2020-02-21 03:10:23

问题


I'd like to do something like this:

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

and have symfony match any routes (.com/secured/something; .com/secured/anything/else) to this one action without defining all the routes manually.

Does symfony support this? I can't think of the terms to search for this.

How can I match and route to this controller action without defining all routes manually, based off the first node (/secured)?


回答1:


/**
 * @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.



来源:https://stackoverflow.com/questions/32575962/symfony-routing-match-anything-after-first-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!