在Symfony2中路由 [英] Routing in Symfony2

查看:94
本文介绍了在Symfony2中路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Symfony2中设置默认路由?

How to setup default routing in Symfony2?

在Symfony1中,它看起来像这样:

In Symfony1 it looked something like this:

homepage:
  url:   /
  param: { module: default, action: index }

default_symfony:
  url:   /symfony/:action/...
  param: { module: default }

default_index:
  url:   /:module
  param: { action: index }

default:
  url:   /:module/:action/...


推荐答案

我正在浏览食谱以寻找答案,并且认为我已经找到了它此处。默认情况下,所有路由参数都有一个隐藏的要求,即它们必须与/字符([^ /] +)以外的任何字符 相匹配,但是可以通过强制匹配来用require关键字覆盖此行为。 任何字符。

I was looking through the cookbook for an answer to this, and think I've found it here. By default, all route parameters have a hidden requirement that they match any character except the / character ([^/]+), but this behaviour can be overridden with the requirements keyword, by forcing it to match any character.

下面的代码应创建一条捕获所有其他路由的默认路由-因此,应排在路由配置的最后,例如以下任何路线均从不匹配。为了确保它也匹配 /,将包括url参数的默认值。

The following should create a default route that catches all others - and as such, should come last in your routing config, as any following routes will never match. To ensure it matches "/" as well, a default value for the url parameter is included.

default_route:
    pattern: /{url}
    defaults: { _controller: AcmeBundle:Default:index, url: "index" }
    requirements:
        url: ".+"

这篇关于在Symfony2中路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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