Skip to content

[3.4] Update logic for controller action arguments #8559

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
Nov 2, 2017
Merged
Show file tree
Hide file tree
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
38 changes: 16 additions & 22 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,8 @@ command:

$ php bin/console debug:autowiring

If you need control over the *exact* value of an argument, you can override your
controller's service config:
If you need control over the *exact* value of an argument, you can :ref:`bind <services-binding>`
the argument by its name:

.. configuration-block::

Expand All @@ -289,13 +289,9 @@ controller's service config:
# explicitly configure the service
AppBundle\Controller\LuckyController:
public: true
tags:
# add multiple tags to control multiple args
- name: controller.service_arguments
action: numberAction
argument: logger
# pass this specific service id
id: monolog.logger.doctrine
bind:
# for any $logger argument, pass this specific service
$logger: '@monolog.logger.doctrine'

.. code-block:: xml

Expand All @@ -311,10 +307,8 @@ controller's service config:

<!-- Explicitly configure the service -->
<service id="AppBundle\Controller\LuckyController" public="true">
<tag
name="controller.service_arguments"
action="numberAction"
argument="logger"
<bind key="$logger"
type="service"
id="monolog.logger.doctrine"
/>
</service>
Expand All @@ -325,25 +319,25 @@ controller's service config:

// app/config/services.php
use AppBundle\Controller\LuckyController;
use Symfony\Component\DependencyInjection\Reference;

$container->register(LuckyController::class)
->setPublic(true)
->addTag('controller.service_arguments', [
'action' => 'numberAction',
'argument' => 'logger',
'id' => 'monolog.logger.doctrine',
])
->setBindings(array(
'$logger' => new Reference('monolog.logger.doctrine'),
))
;

You can of course also use normal :ref:`constructor injection <services-constructor-injection>`
in your controllers.

.. caution::

You can *only* pass *services* to your controller arguments in this way. It's
not possible, for example, to pass a service parameter as a controller argument.
If you need a parameter, use the ``$this->getParameter('kernel.debug')`` shortcut
or pass the value through your controller's ``__construct()`` method.
You can *only* pass *services* to your controller arguments in this way. It's not
possible, for example, to pass a service parameter as a controller argument,
even by using ``bind``. If you need a parameter, use the ``$this->getParameter('kernel.debug')``
shortcut or pass the value through your controller's ``__construct()`` method
and specify its value with ``bind``.

For more information about services, see the :doc:`/service_container` article.

Expand Down
7 changes: 7 additions & 0 deletions service_container.rst
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,13 @@ service whose id is ``monolog.logger.request``.
the *service* whose id is ``monolog.logger.request``, and not just the *string*
``monolog.logger.request``.

.. _services-binding:

Binding Arguments by Name or Type
---------------------------------

You can also use the ``bind`` keyword to bind specific arguments by name or type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a poor bootstrap of the bind section, just so I can link to it. i've commented in the issue about that feature (#8276) to note that this was created.

.. _services-autowire:

The autowire Option
Expand Down