Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit93c35d0

Browse files
committed
minorsymfony#3641 Added some examples to the "services as parameters" section (javiereguiluz)
This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closessymfony#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 | noCommits-------745f3a6 Removed the file path comments because this doc is for the component and it should not make any reference to Symfony6abb310 Added some examples to the "services as parameters" section
2 parents12a6676 +c0d766e commit93c35d0

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

‎components/dependency_injection/parameters.rst

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

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

370+
..code-block::xml
371+
372+
<parameters>
373+
<!-- if 'my_mailer' service isn't defined, an exception will be raised-->
374+
<parameterkey="foo"type="service"id="my_mailer" />
375+
376+
<!-- if 'my_logger' service isn't defined, 'bar' will be null-->
377+
<parameterkey="bar"type="service"id="my_logger"on-invalid="null" />
378+
</parameters>
379+
361380
PHP
362381
~~~
363382

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp