Skip to content

[Components][DependencyInjection] do not reference services in parameters #4222

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

Merged
merged 1 commit into from
Sep 16, 2014
Merged
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
73 changes: 0 additions & 73 deletions components/dependency_injection/parameters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -336,76 +336,3 @@ To disable this behavior, use the ``string`` type:

This is not available for YAML and PHP, because they already have built-in
support for the PHP keywords.

Syntax for Referencing Services
-------------------------------

You can of course also reference services, which looks a bit different in
each format. You can configure the behavior if the referenced service does
not exist. By default, an exception is thrown when a non-existent service
is referenced.

YAML
~~~~

Start the string with ``@`` or ``@?`` to reference a service in YAML.

* ``@mailer`` references the ``mailer`` service. If the service does not
exist, an exception will be thrown;
* ``@?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
converted into the string ``"@mailer"`` instead of referencing the
``mailer`` service.

XML
~~~

In XML, use the ``service`` type. The behavior if the service does not exist
can be specified using the ``on-invalid`` argument. By default, an exception
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
~~~

In PHP, you can use the
:class:`Symfony\\Component\\DependencyInjection\\Reference` class to reference
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
));