Skip to content

[Routing] Added a note about defining route requirements in the container #1827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions book/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

<?xml version="1.0" encoding="UTF-8" ?>

<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">

<route id="contact" pattern="/{_locale}/contact">
<default key="_controller">AcmeDemoBundle:Main:contact</default>
<requirement key="_locale">%locale%</requirement>
</route>
</routes>

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

Expand Down