@@ -8,40 +8,10 @@ You can define parameters in the service container which can then be used
8
8
directly or as part of service definitions. This can help to separate out
9
9
values that you will want to change more regularly.
10
10
11
- Getting and Setting Container Parameters
12
- ----------------------------------------
13
-
14
- Working with container parameters is straightforward using the container's
15
- accessor methods for parameters. You can check if a parameter has been defined
16
- in the container with::
17
-
18
- $container->hasParameter('mailer.transport');
19
-
20
- You can retrieve a parameter set in the container with::
21
-
22
- $container->getParameter('mailer.transport');
23
-
24
- and set a parameter in the container with::
25
-
26
- $container->setParameter('mailer.transport', 'sendmail');
27
-
28
- .. caution ::
29
-
30
- The used ``. `` notation is just a
31
- :ref: `Symfony convention <service-naming-conventions >` to make parameters
32
- easier to read. Parameters are just flat key-value elements, they can't
33
- be organized into a nested array
34
-
35
- .. note ::
36
-
37
- You can only set a parameter before the container is compiled. To learn
38
- more about compiling the container see
39
- :doc: `/components/dependency_injection/compilation `.
40
-
41
11
Parameters in Configuration Files
42
12
---------------------------------
43
13
44
- You can also use the ``parameters `` section of a config file to set parameters:
14
+ Use the ``parameters `` section of a config file to set parameters:
45
15
46
16
.. configuration-block ::
47
17
@@ -66,16 +36,14 @@ You can also use the ``parameters`` section of a config file to set parameters:
66
36
67
37
$container->setParameter('mailer.transport', 'sendmail');
68
38
69
- As well as retrieving the parameter values directly from the container you
70
- can use them in the config files. You can refer to parameters elsewhere
71
- by surrounding them with percent (``% ``) signs, e.g. ``%mailer.transport% ``.
72
- One use for this is to inject the values into your services. This allows
73
- you to configure different versions of services between applications or
74
- multiple services based on the same class but configured differently
75
- within a single application. You could inject the choice of mail transport
76
- into the ``Mailer `` class directly. But declaring it as a parameter makes
77
- it easier to change rather than being tied up and hidden with the service
78
- definition:
39
+ You can refer to parameters elsewhere in any config file by surrounding them
40
+ with percent (``% ``) signs, e.g. ``%mailer.transport% ``. One use for this is
41
+ to inject the values into your services. This allows you to configure different
42
+ versions of services between applications or multiple services based on the
43
+ same class but configured differently within a single application. You could
44
+ inject the choice of mail transport into the ``Mailer `` class directly. But
45
+ declaring it as a parameter makes it easier to change rather than being tied up
46
+ and hidden with the service definition:
79
47
80
48
.. configuration-block ::
81
49
@@ -85,8 +53,8 @@ definition:
85
53
mailer.transport : sendmail
86
54
87
55
services :
88
- mailer :
89
- class : Mailer
56
+ app. mailer :
57
+ class : AppBundle\ Mailer
90
58
arguments : ['%mailer.transport%']
91
59
92
60
.. code-block :: xml
@@ -101,7 +69,7 @@ definition:
101
69
</parameters >
102
70
103
71
<services >
104
- <service id =" mailer" class =" Mailer" >
72
+ <service id =" app. mailer" class =" AppBundle\ Mailer" >
105
73
<argument >%mailer.transport%</argument >
106
74
</service >
107
75
</services >
@@ -113,8 +81,7 @@ definition:
113
81
114
82
$container->setParameter('mailer.transport', 'sendmail');
115
83
116
- $container
117
- ->register('mailer', 'Mailer')
84
+ $container->register('app.mailer', 'AppBundle\Mailer')
118
85
->addArgument('%mailer.transport%');
119
86
120
87
.. caution ::
@@ -139,9 +106,6 @@ definition:
139
106
140
107
<parameter key =" mailer.transport" >sendmail</parameter >
141
108
142
- If you were using this elsewhere as well, then you would only need to change
143
- the parameter value in one place if needed.
144
-
145
109
.. note ::
146
110
147
111
The percent sign inside a parameter or argument, as part of the string,
@@ -161,6 +125,34 @@ the parameter value in one place if needed.
161
125
162
126
->addArgument('http://symfony.com/?foo=%%s&bar=%%d');
163
127
128
+ Getting and Setting Container Parameters in PHP
129
+ -----------------------------------------------
130
+
131
+ Working with container parameters is straightforward using the container's
132
+ accessor methods for parameters::
133
+
134
+ // check if a parameter is defined
135
+ $container->hasParameter('mailer.transport');
136
+
137
+ // get value of a parameter
138
+ $container->getParameter('mailer.transport');
139
+
140
+ // add a new parameter
141
+ $container->setParameter('mailer.transport', 'sendmail');
142
+
143
+ .. caution ::
144
+
145
+ The used ``. `` notation is just a
146
+ :ref: `Symfony convention <service-naming-conventions >` to make parameters
147
+ easier to read. Parameters are just flat key-value elements, they can't
148
+ be organized into a nested array
149
+
150
+ .. note ::
151
+
152
+ You can only set a parameter before the container is compiled. To learn
153
+ more about compiling the container see
154
+ :doc: `/components/dependency_injection/compilation `.
155
+
164
156
.. _component-di-parameters-array :
165
157
166
158
Array Parameters
@@ -175,10 +167,8 @@ for all parameters that are arrays.
175
167
.. code-block :: yaml
176
168
177
169
parameters :
178
- my_mailer.gateways :
179
- - mail1
180
- - mail2
181
- - mail3
170
+ my_mailer.gateways : [mail1, mail2, mail3]
171
+
182
172
my_multilang.language_fallback :
183
173
en :
184
174
- en
@@ -200,11 +190,13 @@ for all parameters that are arrays.
200
190
<parameter >mail2</parameter >
201
191
<parameter >mail3</parameter >
202
192
</parameter >
193
+
203
194
<parameter key =" my_multilang.language_fallback" type =" collection" >
204
195
<parameter key =" en" type =" collection" >
205
196
<parameter >en</parameter >
206
197
<parameter >fr</parameter >
207
198
</parameter >
199
+
208
200
<parameter key =" fr" type =" collection" >
209
201
<parameter >fr</parameter >
210
202
<parameter >en</parameter >
@@ -226,7 +218,7 @@ for all parameters that are arrays.
226
218
Constants as Parameters
227
219
-----------------------
228
220
229
- The container also has support for setting PHP constants as parameters.
221
+ The XML and PHP formats also have support for setting PHP constants as parameters.
230
222
To take advantage of this feature, map the name of your constant to a parameter
231
223
key and define the type as ``constant ``.
232
224
@@ -250,10 +242,10 @@ key and define the type as ``constant``.
250
242
$container->setParameter('global.constant.value', GLOBAL_CONSTANT);
251
243
$container->setParameter('my_class.constant.value', My_Class::CONSTANT_NAME);
252
244
253
- .. note ::
245
+ .. tip ::
254
246
255
- This does not work for YAML configurations. If you're using YAML, you
256
- can import an XML file to take advantage of this functionality:
247
+ If you're using YAML, you can :doc: ` import an XML file < /service_container/import >`
248
+ to take advantage of this functionality:
257
249
258
250
.. code-block :: yaml
259
251
0 commit comments