Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f32aec1

Browse filesBrowse files
committed
[DI][HttpKernel] document Autowire attribute
1 parent 4c9291d commit f32aec1
Copy full SHA for f32aec1

File tree

2 files changed

+90
-1
lines changed
Filter options

2 files changed

+90
-1
lines changed

‎controller.rst

Copy file name to clipboardExpand all lines: controller.rst
+40-1Lines changed: 40 additions & 1 deletion
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

Copy file name to clipboardExpand all lines: service_container.rst
+50Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,56 @@ For a full list of *all* possible services in the container, run:
687687
688688
.. _services-binding:
689689

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

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.