2
2
single: Dependency Injection; Parameters
3
3
4
4
Introduction to Parameters
5
- =================================
5
+ ==========================
6
6
7
7
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
@@ -11,13 +11,13 @@ values that you will want to change more regularly.
11
11
Getting and Setting Container Parameters
12
12
----------------------------------------
13
13
14
- Working with container parameters is straight forward using the container's
14
+ Working with container parameters is straightforward using the container's
15
15
accessor methods for parameters. You can check if a parameter has been defined
16
16
in the container with::
17
17
18
18
$container->hasParameter('mailer.transport');
19
19
20
- You can retrieve parameters set in the container with::
20
+ You can retrieve a parameter set in the container with::
21
21
22
22
$container->getParameter('mailer.transport');
23
23
@@ -29,7 +29,7 @@ and set a parameter in the container with::
29
29
30
30
You can only set a parameter before the container is compiled. To learn
31
31
more about compiling the container see
32
- :doc: `/components/dependency_injection/compilation `
32
+ :doc: `/components/dependency_injection/compilation `.
33
33
34
34
Parameters in Configuration Files
35
35
---------------------------------
@@ -54,14 +54,14 @@ You can also use the ``parameters`` section of a config file to set parameters:
54
54
$container->setParameter('mailer.transport', 'sendmail');
55
55
56
56
As well as retrieving the parameter values directly from the container you
57
- can use them in the config files. You can refer to parameters elsewhere in
58
- the config files by surrounding them with percent (``% ``) signs, e.g.
59
- `` %mailer.transport% ``. One use is for this is to inject the values into your
60
- services. This allows you to configure different versions of services between
61
- applications or multiple services based on the same class but configured
62
- differently within a single application. You could inject the choice of mail
63
- transport into the `` Mailer `` class directly but by making it a parameter it
64
- makes it easier to change rather than being tied up with the service definition:
57
+ can use them in the config files. You can refer to parameters elsewhere by
58
+ surrounding them with percent (``% ``) signs, e.g. `` %mailer.transport% `` .
59
+ One use for this is to inject the values into your services. This allows
60
+ you to configure different versions of services between applications or multiple
61
+ services based on the same class but configured differently within a single
62
+ application. You could inject the choice of mail transport into the `` Mailer ``
63
+ class directly but by making it a parameter. This makes it easier to change
64
+ rather than being tied up and hidden with the service definition:
65
65
66
66
.. configuration-block ::
67
67
@@ -97,8 +97,8 @@ makes it easier to change rather than being tied up with the service definition:
97
97
->register('mailer', 'Mailer')
98
98
->addArgument('%mailer.transport%');
99
99
100
- If we were using this elsewhere as well, then it would only need changing
101
- in one place if a different transport was required .
100
+ If you were using this elsewhere as well, then you would only need to change
101
+ the parameter value in one place if needed .
102
102
103
103
You can also use the parameters in the service definition, for example,
104
104
making the class of a service a parameter:
@@ -164,6 +164,8 @@ making the class of a service a parameter:
164
164
165
165
->addArgument('http://symfony.com/?foo=%%s&bar=%%d');
166
166
167
+ .. _component-di-parameters-array :
168
+
167
169
Array Parameters
168
170
----------------
169
171
@@ -221,6 +223,8 @@ arrays.
221
223
'fr' => array('fr', 'en'),
222
224
));
223
225
226
+ .. _component-di-parameters-constants :
227
+
224
228
Constants as Parameters
225
229
-----------------------
226
230
0 commit comments