Skip to content

Commit a7c791f

Browse files
committed
[DI][HttpKernel] document Autowire attribute
1 parent 3aa06f8 commit a7c791f

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

controller.rst

+40-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ and ``redirect()`` methods::
153153

154154
// redirects to a route and maintains the original query string parameters
155155
return $this->redirectToRoute('blog_show', $request->query->all());
156-
156+
157157
// redirects to the current route (e.g. for Post/Redirect/Get pattern):
158158
return $this->redirectToRoute($request->attributes->get('_route'));
159159

@@ -286,6 +286,45 @@ in your controllers.
286286

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

289+
Autowire Parameter Attribute
290+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291+
292+
.. versionadded:: 6.1
293+
294+
The ``#[Autowire]`` attribute was introduced in Symfony 6.1.
295+
296+
Services that cannot be autowired, :ref:`parameters <service-parameters>` and even
297+
:doc:`complex expressions </service_container/expression_language>` can be bound
298+
to a controller argument with the ``#[Autowire]`` attribute::
299+
300+
use Psr\Log\LoggerInterface;
301+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
302+
use Symfony\Component\HttpFoundation\Response;
303+
// ...
304+
305+
/**
306+
* @Route("/lucky/number/{max}")
307+
*/
308+
public function number(
309+
int $max,
310+
311+
#[Autowire('@monolog.logger.request')]
312+
LoggerInterface $logger,
313+
314+
#[Autowire('%kernel.project_dir%/data')]
315+
string $dataDir,
316+
317+
#[Autowire('%kernel.debug%')]
318+
bool $debugMode,
319+
320+
#[Autowire("@=service("App\\Mail\\MailerConfiguration").getMailerMethod()")]
321+
string $mailerMethod,
322+
): Response
323+
{
324+
$logger->info('We are logging!');
325+
// ...
326+
}
327+
289328
Generating Controllers
290329
----------------------
291330

service_container.rst

+50
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,56 @@ For a full list of *all* possible services in the container, run:
684684
685685
.. _services-binding:
686686

687+
Autowire Parameter Attribute
688+
----------------------------
689+
690+
.. versionadded:: 6.1
691+
692+
The ``#[Autowire]`` attribute was introduced in Symfony 6.1.
693+
694+
For services that cannot be autowired, you can use the ``#[Autowire]`` parameter
695+
attribute to explicitly configure the service::
696+
697+
// src/Service/MessageGenerator.php
698+
namespace App\Service;
699+
700+
use Psr\Log\LoggerInterface;
701+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
702+
703+
class MessageGenerator
704+
{
705+
public function __construct(
706+
#[Autowire('@monolog.logger.request')] private LoggerInterface $logger
707+
) {
708+
}
709+
// ...
710+
}
711+
712+
The ``#[Autowire]`` can also be used for :ref:`parameters <service-parameters>` and even
713+
:doc:`complex expressions </service_container/expression_language>`::
714+
715+
// src/Service/MessageGenerator.php
716+
namespace App\Service;
717+
718+
use Psr\Log\LoggerInterface;
719+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
720+
721+
class MessageGenerator
722+
{
723+
public function __construct(
724+
#[Autowire('%kernel.project_dir%/data')]
725+
private string $dataDir,
726+
727+
#[Autowire('%kernel.debug%')]
728+
private bool $debugMode,
729+
730+
#[Autowire("@=service("App\\Mail\\MailerConfiguration").getMailerMethod()")]
731+
private string $mailerMethod,
732+
) {
733+
}
734+
// ...
735+
}
736+
687737
Binding Arguments by Name or Type
688738
---------------------------------
689739

0 commit comments

Comments
 (0)