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

Skip to content

[DependencyInjection][HttpKernel] document Autowire attribute #16629

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 2 commits into from
May 1, 2022
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
100 changes: 39 additions & 61 deletions controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ and ``redirect()`` methods::

// redirects to a route and maintains the original query string parameters
return $this->redirectToRoute('blog_show', $request->query->all());

// redirects to the current route (e.g. for Post/Redirect/Get pattern):
return $this->redirectToRoute($request->attributes->get('_route'));

Expand Down Expand Up @@ -223,66 +223,44 @@ command:

$ php bin/console debug:autowiring

If you need control over the *exact* value of an argument, you can :ref:`bind <services-binding>`
the argument by its name:

.. configuration-block::

.. code-block:: yaml

# config/services.yaml
services:
# ...

# explicitly configure the service
App\Controller\LuckyController:
tags: [controller.service_arguments]
bind:
# for any $logger argument, pass this specific service
$logger: '@monolog.logger.doctrine'
# for any $projectDir argument, pass this parameter value
$projectDir: '%kernel.project_dir%'

.. code-block:: xml

<!-- config/services.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<!-- ... -->

<!-- Explicitly configure the service -->
<service id="App\Controller\LuckyController">
<tag name="controller.service_arguments"/>
<bind key="$logger"
type="service"
id="monolog.logger.doctrine"
/>
<bind key="$projectDir">%kernel.project_dir%</bind>
</service>
</services>
</container>

.. code-block:: php

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

$container->register(LuckyController::class)
->addTag('controller.service_arguments')
->setBindings([
'$logger' => new Reference('monolog.logger.doctrine'),
'$projectDir' => '%kernel.project_dir%',
])
;

Like with all services, you can also use regular :ref:`constructor injection <services-constructor-injection>`
in your controllers.
.. tip::

If you need control over the *exact* value of an argument, you can use the
``#[Autowire]`` attribute::

// ...
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Response;

class LuckyController extends AbstractController
{
public function number(
int $max,

// inject a specific logger service
#[Autowire('@monolog.logger.request')]
LoggerInterface $logger,

// or inject parameter values
#[Autowire('%kernel.project_dir%')]
string $projectDir
): Response
{
$logger->info('We are logging!');
// ...
}
}

You can read more about this attribute in :ref:`autowire-attribute`.

.. versionadded:: 6.1

The ``#[Autowire]`` attribute was introduced in Symfony 6.1.

Like with all services, you can also use regular
:ref:`constructor injection <services-constructor-injection>` in your
controllers.

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

Expand Down
56 changes: 54 additions & 2 deletions service_container/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -532,15 +532,67 @@ If the argument is named ``$shoutyTransformer``,
But, you can also manually wire any *other* service by specifying the argument
under the arguments key.

.. _autowire-attribute:

Fixing Non-Autowireable Arguments
---------------------------------

Autowiring only works when your argument is an *object*. But if you have a scalar
argument (e.g. a string), this cannot be autowired: Symfony will throw a clear
exception.

To fix this, you can :ref:`manually wire the problematic argument <services-manually-wire-args>`.
You wire up the difficult arguments, Symfony takes care of the rest.
To fix this, you can :ref:`manually wire the problematic argument <services-manually-wire-args>`
in the service configuration. You wire up only the difficult arguments,
Symfony takes care of the rest.

You can also use the ``#[Autowire]`` parameter attribute to configure the
problematic arguments:

// src/Service/MessageGenerator.php
namespace App\Service;

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

class MessageGenerator
{
public function __construct(
#[Autowire('@monolog.logger.request')] LoggerInterface $logger
) {
// ...
}
}

.. versionadded:: 6.1

The ``#[Autowire]`` attribute was introduced in Symfony 6.1.

The ``#[Autowire]`` attribute can also be used for :ref:`parameters <service-parameters>`
and even :doc:`complex expressions </service_container/expression_language>`::

// src/Service/MessageGenerator.php
namespace App\Service;

use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

class MessageGenerator
{
public function __construct(
// use the %...% syntax for parameters
#[Autowire('%kernel.project_dir%/data')]
string $dataDir,

#[Autowire('%kernel.debug%')]
bool $debugMode,

// and @=... for expressions
#[Autowire("@=service("App\\Mail\\MailerConfiguration").getMailerMethod()")]
string $mailerMethod
) {
}
// ...
}

.. _autowiring-calls:

Expand Down