diff --git a/book/routing.rst b/book/routing.rst index c67f93031a6..afb20e69a65 100644 --- a/book/routing.rst +++ b/book/routing.rst @@ -417,6 +417,50 @@ match, giving the ``page`` parameter a value of ``2``. Perfect. | /blog/2 | {page} = 2 | +---------+------------+ +Routing with patterns using parameters from the Service Container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 2.1 + This feature was added in Symfony 2.1 + +You can define patterns using parameters defined in the Service Container. + +.. configuration-block:: + + .. code-block:: yaml + + some_route: + pattern: /%parameter_name% + defaults: { _controller: AcmeDemoBundle:Main:index } + + .. code-block:: xml + + + + + + + AcmeDemoBundle:Main:index + + + + .. code-block:: php + + use Symfony\Component\Routing\RouteCollection; + use Symfony\Component\Routing\Route; + + $collection = new RouteCollection(); + $collection->add('some_route', new Route('/%parameter_name%', array( + '_controller' => 'AcmeDemoBundle:Main:contact', + ))); + + return $collection; + +.. note:: + You can escape a parameter by doubling the ``%``, e.g. ``/%%parameter_name%%`` + .. index:: single: Routing; Requirements @@ -615,6 +659,61 @@ the regular expression ``(en|fr)``. | /es | *won't match this route* | +-----+--------------------------+ +.. index:: + single: Routing; Requirements service + +Requirements using parameters from the Service Container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 2.1 + This feature was added in Symfony 2.1 + +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