Skip to content

Commit bb8f3a6

Browse files
committed
Moved parameters to main topic
1 parent b2f3276 commit bb8f3a6

File tree

3 files changed

+50
-60
lines changed

3 files changed

+50
-60
lines changed

_build/redirection_map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@
253253
/components/dependency_injection/index /components/dependency_injection
254254
/components/dependency_injection/factories /service_container/factories
255255
/components/dependency_injection/lazy_services /service_container/lazy_services
256+
/components/dependency_injection/parameters /service_container/parameters
256257
/components/event_dispatcher/introduction /components/event_dispatcher
257258
/components/expression_language/introduction /components/expression_language
258259
/components/expression_language/index /components/expression_language

service_container.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,6 @@ Learn more
462462

463463
/service_container/*
464464

465-
* :doc:`/components/dependency_injection/parameters`
466-
* :doc:`/components/dependency_injection/definitions`
467-
* :doc:`/components/dependency_injection/factories`
468465
* :doc:`/components/dependency_injection/parentservices`
469466

470467
.. _`service-oriented architecture`: https://en.wikipedia.org/wiki/Service-oriented_architecture

components/dependency_injection/parameters.rst renamed to service_container/parameters.rst

Lines changed: 49 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,10 @@ You can define parameters in the service container which can then be used
88
directly or as part of service definitions. This can help to separate out
99
values that you will want to change more regularly.
1010

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-
4111
Parameters in Configuration Files
4212
---------------------------------
4313

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:
4515

4616
.. configuration-block::
4717

@@ -66,16 +36,14 @@ You can also use the ``parameters`` section of a config file to set parameters:
6636
6737
$container->setParameter('mailer.transport', 'sendmail');
6838
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:
7947

8048
.. configuration-block::
8149

@@ -85,8 +53,8 @@ definition:
8553
mailer.transport: sendmail
8654
8755
services:
88-
mailer:
89-
class: Mailer
56+
app.mailer:
57+
class: AppBundle\Mailer
9058
arguments: ['%mailer.transport%']
9159
9260
.. code-block:: xml
@@ -101,7 +69,7 @@ definition:
10169
</parameters>
10270
10371
<services>
104-
<service id="mailer" class="Mailer">
72+
<service id="app.mailer" class="AppBundle\Mailer">
10573
<argument>%mailer.transport%</argument>
10674
</service>
10775
</services>
@@ -113,8 +81,7 @@ definition:
11381
11482
$container->setParameter('mailer.transport', 'sendmail');
11583
116-
$container
117-
->register('mailer', 'Mailer')
84+
$container->register('app.mailer', 'AppBundle\Mailer')
11885
->addArgument('%mailer.transport%');
11986
12087
.. caution::
@@ -139,9 +106,6 @@ definition:
139106
140107
<parameter key="mailer.transport">sendmail</parameter>
141108
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-
145109
.. note::
146110

147111
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.
161125
162126
->addArgument('http://symfony.com/?foo=%%s&bar=%%d');
163127
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+
164156
.. _component-di-parameters-array:
165157

166158
Array Parameters
@@ -175,10 +167,8 @@ for all parameters that are arrays.
175167
.. code-block:: yaml
176168
177169
parameters:
178-
my_mailer.gateways:
179-
- mail1
180-
- mail2
181-
- mail3
170+
my_mailer.gateways: [mail1, mail2, mail3]
171+
182172
my_multilang.language_fallback:
183173
en:
184174
- en
@@ -200,11 +190,13 @@ for all parameters that are arrays.
200190
<parameter>mail2</parameter>
201191
<parameter>mail3</parameter>
202192
</parameter>
193+
203194
<parameter key="my_multilang.language_fallback" type="collection">
204195
<parameter key="en" type="collection">
205196
<parameter>en</parameter>
206197
<parameter>fr</parameter>
207198
</parameter>
199+
208200
<parameter key="fr" type="collection">
209201
<parameter>fr</parameter>
210202
<parameter>en</parameter>
@@ -226,7 +218,7 @@ for all parameters that are arrays.
226218
Constants as Parameters
227219
-----------------------
228220

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.
230222
To take advantage of this feature, map the name of your constant to a parameter
231223
key and define the type as ``constant``.
232224

@@ -250,10 +242,10 @@ key and define the type as ``constant``.
250242
$container->setParameter('global.constant.value', GLOBAL_CONSTANT);
251243
$container->setParameter('my_class.constant.value', My_Class::CONSTANT_NAME);
252244
253-
.. note::
245+
.. tip::
254246

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:
257249

258250
.. code-block:: yaml
259251

0 commit comments

Comments
 (0)