Skip to content

[Routing] Using Service Container parameters for routes. #1832

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 3 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
99 changes: 99 additions & 0 deletions book/routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

<?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="some_route" pattern="/%parameter_name%">
<default key="_controller">AcmeDemoBundle:Main:index</default>
</route>
</routes>

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

Expand Down Expand Up @@ -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

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