10000 Added some examples to the "services as parameters" section by javiereguiluz · Pull Request #3641 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

Added some examples to the "services as parameters" section #3641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions components/dependency_injection/parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,15 @@ Start the string with ``@`` or ``@?`` to reference a service in YAML.
* ``@?mailer`` references the ``mailer`` service. If the service does not
exist, it will be ignored;

.. code-block:: yaml

parameters:
# if 'my_mailer' service isn't defined, an exception will be raised
foo: @my_mailer

# if 'my_logger' service isn't defined, 'bar' will be null
bar: @?my_logger

.. tip::

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

.. code-block:: xml

<parameters>
<!-- if 'my_mailer' service isn't defined, an exception will be raised -->
<parameter key="foo" type="service" id="my_mailer" />

<!-- if 'my_logger' service isn't defined, 'bar' will be null -->
<parameter key="bar" type="service" id="my_logger" on-invalid="null" />
</parameters>

PHP
~~~

Expand All @@ -367,3 +386,15 @@ In PHP, you can use the
a service. The invalid behavior is configured using the second constructor
argument and constants from
:class:`Symfony\\Component\\DependencyInjection\\ContainerInterface`.

.. code-block:: php

use Symfony\Component\DependencyInjection\Reference;

// if 'my_mailer' service isn't defined, an exception will be raised
$container->setParameter('foo', new Reference('my_mailer'));

// if 'my_logger' service isn't defined, 'bar' will be null
$container->setParameter('bar', new Reference('my_logger',
ContainerInterface::NULL_ON_INVALID_REFERENCE
));
0