Skip to content

Documented the binding of arguments by name and type #10391

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 25, 2018
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
20 changes: 19 additions & 1 deletion service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,10 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
# service that's defined in this file
Psr\Log\LoggerInterface: '@monolog.logger.request'

# optionally you can define both the name and type of the argument to match
string $adminEmail: '[email protected]'
Psr\Log\LoggerInterface $requestLogger: '@monolog.logger.request'

# ...

.. code-block:: xml
Expand All @@ -677,6 +681,13 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
type="service"
id="monolog.logger.request"
/>

<!-- optionally you can define both the name and type of the argument to match -->
<bind key="string $adminEmail">[email protected]</bind>
<bind key="Psr\Log\LoggerInterface $requestLogger"
type="service"
id="monolog.logger.request"
/>
</defaults>

<!-- ... -->
Expand All @@ -696,12 +707,19 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
'$adminEmail' => '[email protected]',
'$requestLogger' => new Reference('monolog.logger.request'),
LoggerInterface::class => new Reference('monolog.logger.request'),
// optionally you can define both the name and type of the argument to match
'string $adminEmail' => '[email protected]',
LoggerInterface::class.' $requestLogger' => new Reference('monolog.logger.request'),
))
;

By putting the ``bind`` key under ``_defaults``, you can specify the value of *any*
argument for *any* service defined in this file! You can bind arguments by name
(e.g. ``$adminEmail``) or by type (e.g. ``Psr\Log\LoggerInterface``).
(e.g. ``$adminEmail``), by type (e.g. ``Psr\Log\LoggerInterface``) or both
(e.g. ``Psr\Log\LoggerInterface $requestLogger``).

.. versionadded:: 4.2
The feature to bind arguments by name and type was introduced in Symfony 4.2.

The ``bind`` config can also be applied to specific services or when loading many
services at once (i.e. :ref:`service-psr4-loader`).
Expand Down