Symfony2 路由全局 {_locale} 要求 [英] Symfony2 Route global {_locale} requirements
问题描述
我在 routing.yml 中指定了每条路由中的参数 _locale 要求,我认为这必须是为了简化这种情况.
I have in my routing.yml specified the parameter _locale requirements in every single route and I think that must be something to simplify this situation.
路由.yml
ProjectBaseBundle_index:
pattern: /{_locale}
defaults: { _controller: ProjectBaseBundle:Default:index }
requirements:
_locale: en|es
ProjectBaseBundle_privacy:
pattern: /privacy/{_locale}
defaults: { _controller: ProjectBaseBundle:Default:privacy }
requirements:
_locale: en|es
.....
ProjectBaseBundle_legal:
pattern: /legal/{_locale}
defaults: { _controller: ProjectBaseBundle:Default:legal }
requirements:
_locale: en|es
我使用的是 Symfony2.1 beta 3
I am using Symfony2.1 beta 3
是否可以为我的所有路线指定全局 _locale 要求?
推荐答案
我发现了一种方法:
使用主"路由导入路由配置.由于我的包通常有太多信息,我一直在不同的模块"中分离控制器、资源和路由.由于这种方法,我发现了这一点:
Using a "master" routing to import the routing config. As my bundles usually have too much information, I have been separating the controllers, resources and routes in differents "modules". As result of that approach, I have discovered this:
主路由.yml
ProjectBaseBundle_default:
resource: "@ProjectBaseBundle/Resources/config/routing-default.yml"
prefix: /{_locale}/project/
requirements:
_locale: en|es|de|fr
子路由-default.yml
ProjectBaseBundle_default_privacy:
pattern: /privacy
defaults: { _controller: ProjectBaseBundle:Default:privacy }
ProjectBaseBundle_default_legal:
pattern: /legal
defaults: { _controller: ProjectBaseBundle:Default:legal }
ProjectBaseBundle_default_usage:
pattern: /usage
defaults: { _controller: ProjectBaseBundle:Default:usage }
通过这个路由配置,我最大限度地减少了需要编写语言环境要求的地方.
With this routing config, I minimize the places where need to write the locale requirements.
这篇关于Symfony2 路由全局 {_locale} 要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!