1
1
.. index ::
2
- single: Routing; _service_container_parameters
2
+ single: Routing; Service Container Parameters
3
3
4
- How to use Service Container parameters in your routes
4
+ How to use Service Container Parameters in your Routes
5
5
======================================================
6
6
7
7
.. versionadded :: 2.1
8
- This feature was added in Symfony 2.1
8
+ The ability to use parameters in your routes was added in Symfony 2.1.
9
9
10
- Sometimes you may find useful to make some parts of your routes
10
+ Sometimes you may find it useful to make some parts of your routes
11
11
globally configurable. For instance, if you build an internationalized
12
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 .
13
+ add a requirement to your routes to prevent a user from matching a locale
14
+ other than the locales your support .
15
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.
16
+ You *could * hardcode your ``_locale `` requirement in all your routes. But
17
+ a better solution is to use a configurable service container parameter right
18
+ inside your routing configuration:
24
19
25
20
.. configuration-block ::
26
21
@@ -60,17 +55,39 @@ Here you have an example on how to make the ``_locale`` of your routes configura
60
55
61
56
return $collection;
62
57
63
- Easy like that, then simply define the ``acme_demo.locales `` parameter in your container.
58
+ You can now control and set the ``acme_demo.locales `` parameter somewhere
59
+ in your container:
60
+
61
+ .. configuration-block ::
62
+
63
+ .. code-block :: yaml
64
+
65
+ # app/config/config.yml
66
+ parameters :
67
+ acme_demo.locales : en|es
64
68
65
- You can also define patterns which use parameters defined in the Service Container.
69
+ .. code-block :: xml
70
+
71
+ <!-- app/config/config.xml -->
72
+ <parameters >
73
+ <parameter key =" acme_demo.locales" >en|es</parameter >
74
+ </parameters >
75
+
76
+ .. code-block :: php
77
+
78
+ # app/config/config.php
79
+ $container->setParameter('acme_demo.locales', 'en|es');
80
+
81
+ You can also use a parameter to define your route pattern (or part of your
82
+ pattern):
66
83
67
84
.. configuration-block ::
68
85
69
86
.. code-block :: yaml
70
87
71
88
some_route :
72
- pattern : /%acme_demo.parameter_name%
73
- defaults : { _controller: AcmeDemoBundle:Main:index }
89
+ pattern : /%acme_demo.route_prefix%/contact
90
+ defaults : { _controller: AcmeDemoBundle:Main:contact }
74
91
75
92
.. code-block :: xml
76
93
@@ -80,8 +97,8 @@ You can also define patterns which use parameters defined in the Service Contain
80
97
xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
81
98
xsi : schemaLocation =" http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd" >
82
99
83
- <route id =" some_route" pattern =" /%acme_demo.parameter_name% " >
84
- <default key =" _controller" >AcmeDemoBundle:Main:index </default >
100
+ <route id =" some_route" pattern =" /%acme_demo.route_prefix%/contact " >
101
+ <default key =" _controller" >AcmeDemoBundle:Main:contact </default >
85
102
</route >
86
103
</routes >
87
104
@@ -91,12 +108,14 @@ You can also define patterns which use parameters defined in the Service Contain
91
108
use Symfony\Component\Routing\Route;
92
109
93
110
$collection = new RouteCollection();
94
- $collection->add('some_route', new Route('/%acme_demo.parameter_name% ', array(
111
+ $collection->add('some_route', new Route('/%acme_demo.route_prefix%/contact ', array(
95
112
'_controller' => 'AcmeDemoBundle:Main:contact',
96
113
)));
97
114
98
115
return $collection;
99
116
100
117
.. note ::
101
- You can escape a parameter by doubling the ``% ``, e.g. ``/%%acme_demo.parameter_name%% ``
102
118
119
+ Just like in normal service container configuration files, if you actually
120
+ need a ``% `` in your route, you can escape the percent sign by doubling
121
+ it, e.g. ``/score-50%% ``, which would resolve to ``/score-50% ``.
0 commit comments