Symfony2 Route global {_locale} requirements

只谈情不闲聊 提交于 2019-11-30 13:25:43

问题


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.

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

Is possible to specify a global _locale requirements for all my routes?


回答1:


I have discovered a way to do that:

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:

Master routing.yml

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 config, I minimize the places where need to write the locale requirements.




回答2:


Take a look at this discussion:

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

It seems that it is possible to do something similar to what you need, only that the {_locale} parameter is specified at the beginning of the route, not at the end. Also you would need to be running beta 4 of the 2.1 version of symfony (according to Fabien)



来源:https://stackoverflow.com/questions/11781287/symfony2-route-global-locale-requirements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!