Skip to content

Commit

Permalink
minor #3641 Added some examples to the "services as parameters" secti…
Browse files Browse the repository at this point in the history
…on (javiereguiluz)

This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #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
  • Loading branch information
weaverryan committed Mar 19, 2014
2 parents 12a6676 + c0d766e commit 93c35d0
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions components/dependency_injection/parameters.rst
Expand Up @@ -343,6 +343,15 @@ Start the string with ``@`` or ``@?`` to reference a service in YAML.
* ``@?mailer`` references the ``mailer`` service. If the service does not * ``@?mailer`` references the ``mailer`` service. If the service does not
exist, it will be ignored; 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:: .. tip::


Use ``@@`` to escape the ``@`` symbol in YAML. ``@@mailer`` will be Use ``@@`` to escape the ``@`` symbol in YAML. ``@@mailer`` will be
Expand All @@ -358,6 +367,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 of the missing service) or ``ignored`` (very similar, except if used on a
method call, the method call is removed). 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 PHP
~~~ ~~~


Expand All @@ -366,3 +385,15 @@ In PHP, you can use the
a service. The invalid behavior is configured using the second constructor a service. The invalid behavior is configured using the second constructor
argument and constants from argument and constants from
:class:`Symfony\\Component\\DependencyInjection\\ContainerInterface`. :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 comments on commit 93c35d0

Please sign in to comment.