php - Symfony 路由 : match anything after first node

标签 php symfony routing

我想做这样的事情:

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

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

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

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

最佳答案

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

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

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

结果将是:

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

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

关于php - Symfony 路由 : match anything after first node,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32575962/

相关文章:

php - 我无法添加到本地数据库,无法运行查询 : SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

php - 这个 GROUP BY 查询有什么问题?

php - Symfony2 中的自定义配置

php - require_once() 和 autoloader - 如何加载平面 php?

ruby-on-rails - 如何使用 Devise 中的 delete 方法进行注销? rails 路由失败(执行 get)

php - 首次尝试使用 PHP/MySQL 进行 OOP

mysql - Symfony2 应用程序如何使用 MySQL 内存存储引擎?

php - 如何忽略 Laravel 路由中的站点地图 url?

node.js - 我使用 express for node.js 得到冒号和我的参数

PHP - 如何测试 $_SERVER ['HTTP_ACCEPT_LANGUAGE' ]