8000 Added some examples to the "services as parameters" section · dunglas/symfony-docs@fabdd8a · GitHub
[go: up one dir, main page]

Skip to content

Commit fabdd8a

Browse files
javiereguiluzweaverryan
authored andcommitted
Added some examples to the "services as parameters" section
1 parent 12a6676 commit fabdd8a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

components/dependency_injection/parameters.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,16 @@ 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+
# app/config/config.yml
349+
parameters:
350+
# if 'my_mailer' service isn't defined, an exception will be raised
351+
foo: @my_mailer
352+
353+
# if 'my_logger' service isn't defined, 'bar' will be null
354+
bar: @?my_logger
355+
346356
.. tip::
347357

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

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

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

0 commit comments

Comments
 (0)
0