From f6daf71d551484e749c9af579dd8fe0513633066 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 22 May 2018 14:00:15 +0200 Subject: [PATCH] add missing argument binding section --- service_container.rst | 73 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/service_container.rst b/service_container.rst index 774b1e3daa1..9e1fffcc231 100644 --- a/service_container.rst +++ b/service_container.rst @@ -694,7 +694,78 @@ service whose id is ``monolog.logger.request``. Binding Arguments by Name or Type --------------------------------- -You can also use the ``bind`` keyword to bind specific arguments by name or type. +You can also use the ``bind`` keyword to bind specific arguments by name or type: + +.. configuration-block:: + + .. code-block:: yaml + + # config/services.yaml + services: + _defaults: + bind: + # pass this value to any $adminEmail argument for any service + # that's defined in this file (including controller arguments) + $adminEmail: 'manager@example.com' + + # pass this service to any $requestLogger argument for any + # service that's defined in this file + $requestLogger: '@monolog.logger.request' + + # pass this service for any LoggerInterface type-hint for any + # service that's defined in this file + Psr\Log\LoggerInterface: '@monolog.logger.request' + + # ... + + .. code-block:: xml + + + + + + + + manager@example.com + + + + + + + + + .. code-block:: php + + // config/services.php + use App\Controller\LuckyController; + use Symfony\Component\DependencyInjection\Reference; + use Psr\Log\LoggerInterface; + + $container->register(LuckyController::class) + ->setPublic(true) + ->setBindings(array( + '$adminEmail' => 'manager@example.com', + '$requestLogger' => new Reference('monolog.logger.request'), + LoggerInterface::class => 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``). + +The ``bind`` config can be also be applied to specific services or when loading many +services at once (i.e. :ref:`service-psr4-loader`). .. _services-autowire: