Symfony2 Route global requirements {_locale}

I have in my routing.yml the parameter _locale of requirements in each separate route, and I think that this should be something that would simplify this situation.

routing.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 

I am using Symfony2.1 beta 3

Can I specify global llocale requirements for all my routes?

+7
source share
2 answers

I found a way to do this:

Use "master" routing to import a routing configuration. Since my packages usually have too much information, I share controllers, resources and routes in different β€œmodules”. As a result of this approach, I discovered this:

Routing.yml wizard

 ProjectBaseBundle_default: resource: "@ProjectBaseBundle/Resources/config/routing-default.yml" prefix: /{_locale}/project/ requirements: _locale: en|es|de|fr 

Child routing-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 configuration, I minimize the places where you need to write down the language requirements.

+11
source

Take a look at this discussion:

https://groups.google.com/forum/#!topic/symfony-devs/6oxsa7whBps

It seems that you can do something similar to what you need, only that the parameter {_locale} is indicated at the beginning of the route, and not at the end. You will also need to run the beta version of symfony version 2.1 (according to Fabien)

0
source

All Articles