Skip to content

[Cookbook][Routing] How to use Service Container parameters. #1834

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

Merged
merged 1 commit into from
Oct 23, 2012
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions cookbook/routing/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ Routing
slash_in_parameter
redirect_in_config
method_parameters
service_container_parameters
102 changes: 102 additions & 0 deletions cookbook/routing/service_container_parameters.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
.. index::
single: Routing; _service_container_parameters

How to use Service Container parameters in your routes
======================================================

.. versionadded:: 2.1
This feature was added in Symfony 2.1

Sometimes you may find useful to make some parts of your routes
globally configurable. For instance, if you build an internationalized
site, you'll probably start with one or two locales. Surely you'll
add requirements to avoid a user specify a locale other than those you
support, or simple to increase the power of your routes.

Suppose that you have a lot of routes and you want to add yet another locale
to your application. If you hardcode the requirements directly in your code,
then you'll need to search everywhere and change the requirements.

Then, why not use a configurable parameter, defined in the Service Container
and make your life easier?

Here you have an example on how to make the ``_locale`` of your routes configurable.

.. configuration-block::

.. code-block:: yaml

contact:
pattern: /{_locale}/contact
defaults: { _controller: AcmeDemoBundle:Main:contact }
requirements:
_locale: %acme_demo.locales%

.. 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">%acme_demo.locales%</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' => '%acme_demo.locales%',
)));

return $collection;

Easy like that, then simply define the ``acme_demo.locales`` parameter in your container.

You can also define patterns which use parameters defined in the Service Container.

.. configuration-block::

.. code-block:: yaml

some_route:
pattern: /%acme_demo.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="/%acme_demo.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('/%acme_demo.parameter_name%', array(
'_controller' => 'AcmeDemoBundle:Main:contact',
)));

return $collection;

.. note::
You can escape a parameter by doubling the ``%``, e.g. ``/%%acme_demo.parameter_name%%``