Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 033e8ea

Browse files
committed
minor #9804 add missing argument binding section (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- add missing argument binding section Commits ------- f6daf71 add missing argument binding section
2 parents 6dfd966 + f6daf71 commit 033e8ea

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

service_container.rst

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,78 @@ service whose id is ``monolog.logger.request``.
694694
Binding Arguments by Name or Type
695695
---------------------------------
696696

697-
You can also use the ``bind`` keyword to bind specific arguments by name or type.
697+
You can also use the ``bind`` keyword to bind specific arguments by name or type:
698+
699+
.. configuration-block::
700+
701+
.. code-block:: yaml
702+
703+
# config/services.yaml
704+
services:
705+
_defaults:
706+
bind:
707+
# pass this value to any $adminEmail argument for any service
708+
# that's defined in this file (including controller arguments)
709+
$adminEmail: '[email protected]'
710+
711+
# pass this service to any $requestLogger argument for any
712+
# service that's defined in this file
713+
$requestLogger: '@monolog.logger.request'
714+
715+
# pass this service for any LoggerInterface type-hint for any
716+
# service that's defined in this file
717+
Psr\Log\LoggerInterface: '@monolog.logger.request'
718+
719+
# ...
720+
721+
.. code-block:: xml
722+
723+
<!-- config/services.xml -->
724+
<?xml version="1.0" encoding="UTF-8" ?>
725+
<container xmlns="http://symfony.com/schema/dic/services"
726+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
727+
xsi:schemaLocation="http://symfony.com/schema/dic/services
728+
http://symfony.com/schema/dic/services/services-1.0.xsd">
729+
730+
<services>
731+
<defaults autowire="true" autoconfigure="true" public="false">
732+
<bind key="$adminEmail">[email protected]</bind>
733+
<bind key="$requestLogger"
734+
type="service"
735+
id="monolog.logger.request"
736+
/>
737+
<bind key="Psr\Log\LoggerInterface"
738+
type="service"
739+
id="monolog.logger.request"
740+
/>
741+
</defaults>
742+
743+
<!-- ... -->
744+
</services>
745+
</container>
746+
747+
.. code-block:: php
748+
749+
// config/services.php
750+
use App\Controller\LuckyController;
751+
use Symfony\Component\DependencyInjection\Reference;
752+
use Psr\Log\LoggerInterface;
753+
754+
$container->register(LuckyController::class)
755+
->setPublic(true)
756+
->setBindings(array(
757+
'$adminEmail' => '[email protected]',
758+
'$requestLogger' => new Reference('monolog.logger.request'),
759+
LoggerInterface::class => new Reference('monolog.logger.request'),
760+
))
761+
;
762+
763+
By putting the ``bind`` key under ``_defaults``, you can specify the value of *any*
764+
argument for *any* service defined in this file! You can bind arguments by name
765+
(e.g. ``$adminEmail``) or by type (e.g. ``Psr\Log\LoggerInterface``).
766+
767+
The ``bind`` config can be also be applied to specific services or when loading many
768+
services at once (i.e. :ref:`service-psr4-loader`).
698769

699770
.. _services-autowire:
700771

0 commit comments

Comments
 (0)