Symfony路由:在第一个节点之后匹配任何内容 [英] Symfony routing: match anything after first node

查看:68
本文介绍了Symfony路由:在第一个节点之后匹配任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做这样的事情:

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

并让symfony匹配任何路线( .com / secured / something ; .com / secured / anything / else )执行此操作,而无需手动定义所有路由。

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

symfony支持吗?我想不出要搜索的术语。

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)?

推荐答案

/**
 * @Route("/secured/{anything}", name="_secured", defaults={"anything" = null}, requirements={"anything"=".+"})
 */

public function securedAction($anything){
    //return secured JS frontend 
}

名称-只是路线名称。

默认值-如果您未在网址中提供参数,则可以在此处设置参数的默认值: / secured /

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

要求-参数要求,在这种情况下,任何内容可以包含斜线: http://symfony.com/ doc / current / cookbook / rout ing / slash_in_parameter.html ,但是您必须自己执行控制器操作:

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:

例如,如果您提供url: /安全的/任何东西/ otherother_thing / one_more_thing

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

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

,结果将是:

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

/ secured / 将是一个参数 $ anything

这篇关于Symfony路由:在第一个节点之后匹配任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆