diff --git a/book/routing.rst b/book/routing.rst index 35d521436b7..dd18fd3752b 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -612,6 +612,58 @@ the regular expression ``(en|fr)``. | /es | *won't match this route* | +-----+--------------------------+ +.. index:: + single: Routing; Requirements service + +Requirements defined in the Service Container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If, for some reason, you need to define some configurable requirements, you can +use a parameter from the Service Container. For instance, if you have a +``_locale`` parameter in the routes and you like it to be configurable, +you can do this: + +.. configuration-block:: + + .. code-block:: yaml + + contact: + pattern: /{_locale}/contact + defaults: { _controller: AcmeDemoBundle:Main:contact } + requirements: + _locale: %locale% + + .. code-block:: xml + + + + + + + AcmeDemoBundle:Main:contact + %locale% + + + + .. code-block:: php + + use Symfony\Component\Routing\RouteCollection; + use Symfony\Component\Routing\Route; + + $collection = new RouteCollection(); + $collection->add('contact', new Route('/{_locale}/contact', array( + '_controller' => 'AcmeDemoBundle:Main:contact', + ), array( + '_locale' => '%locale%', + ))); + + return $collection; + +Then just define in the container the locale parameter. This is quite useful +if you don't want to search all your code only to change a simple requirement. + .. index:: single: Routing; Method requirement