Skip to content

Commit 30216b9

Browse files
committed
Merge branch 'cookbook_routing_service_container_parameters' of github.com:nomack84/symfony-docs into nomack84-cookbook_routing_service_container_parameters
2 parents 0196aaa + 0b268ee commit 30216b9

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

cookbook/routing/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Routing
88
slash_in_parameter
99
redirect_in_config
1010
method_parameters
11+
service_container_parameters
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
.. index::
2+
single: Routing; _service_container_parameters
3+
4+
How to use Service Container parameters in your routes
5+
======================================================
6+
7+
.. versionadded:: 2.1
8+
This feature was added in Symfony 2.1
9+
10+
Sometimes you may find useful to make some parts of your routes
11+
globally configurable. For instance, if you build an internationalized
12+
site, you'll probably start with one or two locales. Surely you'll
13+
add requirements to avoid a user specify a locale other than those you
14+
support, or simple to increase the power of your routes.
15+
16+
Suppose that you have a lot of routes and you want to add yet another locale
17+
to your application. If you hardcode the requirements directly in your code,
18+
then you'll need to search everywhere and change the requirements.
19+
20+
Then, why not use a configurable parameter, defined in the Service Container
21+
and make your life easier?
22+
23+
Here you have an example on how to make the ``_locale`` of your routes configurable.
24+
25+
.. configuration-block::
26+
27+
.. code-block:: yaml
28+
29+
contact:
30+
pattern: /{_locale}/contact
31+
defaults: { _controller: AcmeDemoBundle:Main:contact }
32+
requirements:
33+
_locale: %acme_demo.locales%
34+
35+
.. code-block:: xml
36+
37+
<?xml version="1.0" encoding="UTF-8" ?>
38+
39+
<routes xmlns="http://symfony.com/schema/routing"
40+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
41+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
42+
43+
<route id="contact" pattern="/{_locale}/contact">
44+
<default key="_controller">AcmeDemoBundle:Main:contact</default>
45+
<requirement key="_locale">%acme_demo.locales%</requirement>
46+
</route>
47+
</routes>
48+
49+
.. code-block:: php
50+
51+
use Symfony\Component\Routing\RouteCollection;
52+
use Symfony\Component\Routing\Route;
53+
54+
$collection = new RouteCollection();
55+
$collection->add('contact', new Route('/{_locale}/contact', array(
56+
'_controller' => 'AcmeDemoBundle:Main:contact',
57+
), array(
58+
'_locale' => '%acme_demo.locales%',
59+
)));
60+
61+
return $collection;
62+
63+
Easy like that, then simply define the ``acme_demo.locales`` parameter in your container.
64+
65+
You can also define patterns which use parameters defined in the Service Container.
66+
67+
.. configuration-block::
68+
69+
.. code-block:: yaml
70+
71+
some_route:
72+
pattern: /%acme_demo.parameter_name%
73+
defaults: { _controller: AcmeDemoBundle:Main:index }
74+
75+
.. code-block:: xml
76+
77+
<?xml version="1.0" encoding="UTF-8" ?>
78+
79+
<routes xmlns="http://symfony.com/schema/routing"
80+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
81+
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
82+
83+
<route id="some_route" pattern="/%acme_demo.parameter_name%">
84+
<default key="_controller">AcmeDemoBundle:Main:index</default>
85+
</route>
86+
</routes>
87+
88+
.. code-block:: php
89+
90+
use Symfony\Component\Routing\RouteCollection;
91+
use Symfony\Component\Routing\Route;
92+
93+
$collection = new RouteCollection();
94+
$collection->add('some_route', new Route('/%acme_demo.parameter_name%', array(
95+
'_controller' => 'AcmeDemoBundle:Main:contact',
96+
)));
97+
98+
return $collection;
99+
100+
.. note::
101+
You can escape a parameter by doubling the ``%``, e.g. ``/%%acme_demo.parameter_name%%``
102+

0 commit comments

Comments
 (0)