Skip to content

Commit 6abb310

Browse files
committed
Added some examples to the "services as parameters" section
1 parent a21fb26 commit 6abb310

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

components/dependency_injection/parameters.rst

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

347+
.. code-block:: yaml
348+
349+
# app/config/config.yml
350+
parameters:
351+
# if 'my_mailer' service isn't defined, an exception will be raised
352+
foo: @my_mailer
353+
354+
# if 'my_logger' service isn't defined, 'bar' will be null
355+
bar: @?my_logger
356+
347357
.. tip::
348358

349359
Use ``@@`` to escape the ``@`` symbol in YAML. ``@@mailer`` will be
@@ -359,6 +369,17 @@ is thrown. Valid values for ``on-invalid`` are ``null`` (uses ``null`` in place
359369
of the missing service) or ``ignored`` (very similar, except if used on a
360370
method call, the method call is removed).
361371

372+
.. code-block:: xml
373+
374+
<!-- app/config/config.xml -->
375+
<parameters>
376+
<!-- if 'my_mailer' service isn't defined, an exception will be raised -->
377+
<parameter key="foo" type="service" id="my_mailer" />
378+
379+
<!-- if 'my_logger' service isn't defined, 'bar' will be null -->
380+
<parameter key="bar" type="service" id="my_logger" on-invalid="null" />
381+
</parameters>
382+
362383
PHP
363384
~~~
364385

@@ -367,3 +388,16 @@ In PHP, you can use the
367388
a service. The invalid behavior is configured using the second constructor
368389
argument and constants from
369390
:class:`Symfony\\Component\\DependencyInjection\\ContainerInterface`.
391+
392+
.. code-block:: php
393+
394+
// app/config/config.php
395+
use Symfony\Component\DependencyInjection\Reference;
396+
397+
// if 'my_mailer' service isn't defined, an exception will be raised
398+
$container->setParameter('foo', new Reference('my_mailer'));
399+
400+
// if 'my_logger' service isn't defined, 'bar' will be null
401+
$container->setParameter('bar', new Reference('my_logger',
402+
ContainerInterface::NULL_ON_INVALID_REFERENCE
403+
));

0 commit comments

Comments
 (0)