Skip to content

Commit 93c35d0

Browse files
committed
minor symfony#3641 Added some examples to the "services as parameters" section (javiereguiluz)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes symfony#3641). Discussion ---------- Added some examples to the "services as parameters" section | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | 2.3+ | Fixed tickets | no Commits ------- 745f3a6 Removed the file path comments because this doc is for the component and it should not make any reference to Symfony 6abb310 Added some examples to the "services as parameters" section
2 parents 12a6676 + c0d766e commit 93c35d0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

components/dependency_injection/parameters.rst

+31
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,15 @@ Start the string with ``@`` or ``@?`` to reference a service in YAML.
343343
* ``@?mailer`` references the ``mailer`` service. If the service does not
344344
exist, it will be ignored;
345345

346+
.. code-block:: yaml
347+
348+
parameters:
349+
# if 'my_mailer' service isn't defined, an exception will be raised
350+
foo: @my_mailer
351+
352+
# if 'my_logger' service isn't defined, 'bar' will be null
353+
bar: @?my_logger
354+
346355
.. tip::
347356

348357
Use ``@@`` to escape the ``@`` symbol in YAML. ``@@mailer`` will be
@@ -358,6 +367,16 @@ is thrown. Valid values for ``on-invalid`` are ``null`` (uses ``null`` in place
358367
of the missing service) or ``ignored`` (very similar, except if used on a
359368
method call, the method call is removed).
360369

370+
.. code-block:: xml
371+
372+
<parameters>
373+
<!-- if 'my_mailer' service isn't defined, an exception will be raised -->
374+
<parameter key="foo" type="service" id="my_mailer" />
375+
376+
<!-- if 'my_logger' service isn't defined, 'bar' will be null -->
377+
<parameter key="bar" type="service" id="my_logger" on-invalid="null" />
378+
</parameters>
379+
361380
PHP
362381
~~~
363382

@@ -366,3 +385,15 @@ In PHP, you can use the
366385
a service. The invalid behavior is configured using the second constructor
367386
argument and constants from
368387
:class:`Symfony\\Component\\DependencyInjection\\ContainerInterface`.
388+
389+
.. code-block:: php
390+
391+
use Symfony\Component\DependencyInjection\Reference;
392+
393+
// if 'my_mailer' service isn't defined, an exception will be raised
394+
$container->setParameter('foo', new Reference('my_mailer'));
395+
396+
// if 'my_logger' service isn't defined, 'bar' will be null
397+
$container->setParameter('bar', new Reference('my_logger',
398+
ContainerInterface::NULL_ON_INVALID_REFERENCE
399+
));

0 commit comments

Comments
 (0)