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 f5785b2

Browse filesBrowse files
committed
feature #16629 [DependencyInjection][HttpKernel] document Autowire attribute (kbond)
This PR was merged into the 6.1 branch. Discussion ---------- [DependencyInjection][HttpKernel] document `Autowire` attribute Symfony PRs: symfony/symfony#45657 & symfony/symfony#45783 Closes #16625. Closes #16636 Commits ------- bf8be7e Move the autowire attribute sections f32aec1 [DI][HttpKernel] document `Autowire` attribute
2 parents 7d6d2f6 + bf8be7e commit f5785b2
Copy full SHA for f5785b2

File tree

2 files changed

+93
-63
lines changed
Filter options

2 files changed

+93
-63
lines changed

‎controller.rst

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

@@ -223,66 +223,44 @@ command:
223223
224224
$ php bin/console debug:autowiring
225225
226-
If you need control over the *exact* value of an argument, you can :ref:`bind <services-binding>`
227-
the argument by its name:
228-
229-
.. configuration-block::
230-
231-
.. code-block:: yaml
232-
233-
# config/services.yaml
234-
services:
235-
# ...
236-
237-
# explicitly configure the service
238-
App\Controller\LuckyController:
239-
tags: [controller.service_arguments]
240-
bind:
241-
# for any $logger argument, pass this specific service
242-
$logger: '@monolog.logger.doctrine'
243-
# for any $projectDir argument, pass this parameter value
244-
$projectDir: '%kernel.project_dir%'
245-
246-
.. code-block:: xml
247-
248-
<!-- config/services.xml -->
249-
<?xml version="1.0" encoding="UTF-8" ?>
250-
<container xmlns="http://symfony.com/schema/dic/services"
251-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
252-
xsi:schemaLocation="http://symfony.com/schema/dic/services
253-
https://symfony.com/schema/dic/services/services-1.0.xsd">
254-
255-
<services>
256-
<!-- ... -->
257-
258-
<!-- Explicitly configure the service -->
259-
<service id="App\Controller\LuckyController">
260-
<tag name="controller.service_arguments"/>
261-
<bind key="$logger"
262-
type="service"
263-
id="monolog.logger.doctrine"
264-
/>
265-
<bind key="$projectDir">%kernel.project_dir%</bind>
266-
</service>
267-
</services>
268-
</container>
269-
270-
.. code-block:: php
271-
272-
// config/services.php
273-
use App\Controller\LuckyController;
274-
use Symfony\Component\DependencyInjection\Reference;
275-
276-
$container->register(LuckyController::class)
277-
->addTag('controller.service_arguments')
278-
->setBindings([
279-
'$logger' => new Reference('monolog.logger.doctrine'),
280-
'$projectDir' => '%kernel.project_dir%',
281-
])
282-
;
283-
284-
Like with all services, you can also use regular :ref:`constructor injection <services-constructor-injection>`
285-
in your controllers.
226+
.. tip::
227+
228+
If you need control over the *exact* value of an argument, you can use the
229+
``#[Autowire]`` attribute::
230+
231+
// ...
232+
use Psr\Log\LoggerInterface;
233+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
234+
use Symfony\Component\HttpFoundation\Response;
235+
236+
class LuckyController extends AbstractController
237+
{
238+
public function number(
239+
int $max,
240+
241+
// inject a specific logger service
242+
#[Autowire('@monolog.logger.request')]
243+
LoggerInterface $logger,
244+
245+
// or inject parameter values
246+
#[Autowire('%kernel.project_dir%')]
247+
string $projectDir
248+
): Response
249+
{
250+
$logger->info('We are logging!');
251+
// ...
252+
}
253+
}
254+
255+
You can read more about this attribute in :ref:`autowire-attribute`.
256+
257+
.. versionadded:: 6.1
258+
259+
The ``#[Autowire]`` attribute was introduced in Symfony 6.1.
260+
261+
Like with all services, you can also use regular
262+
:ref:`constructor injection <services-constructor-injection>` in your
263+
controllers.
286264

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

‎service_container/autowiring.rst

Copy file name to clipboardExpand all lines: service_container/autowiring.rst
+54-2Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,67 @@ If the argument is named ``$shoutyTransformer``,
532532
But, you can also manually wire any *other* service by specifying the argument
533533
under the arguments key.
534534

535+
.. _autowire-attribute:
536+
535537
Fixing Non-Autowireable Arguments
536538
---------------------------------
537539

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

542-
To fix this, you can :ref:`manually wire the problematic argument <services-manually-wire-args>`.
543-
You wire up the difficult arguments, Symfony takes care of the rest.
544+
To fix this, you can :ref:`manually wire the problematic argument <services-manually-wire-args>`
545+
in the service configuration. You wire up only the difficult arguments,
546+
Symfony takes care of the rest.
547+
548+
You can also use the ``#[Autowire]`` parameter attribute to configure the
549+
problematic arguments:
550+
551+
// src/Service/MessageGenerator.php
552+
namespace App\Service;
553+
554+
use Psr\Log\LoggerInterface;
555+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
556+
557+
class MessageGenerator
558+
{
559+
public function __construct(
560+
#[Autowire('@monolog.logger.request')] LoggerInterface $logger
561+
) {
562+
// ...
563+
}
564+
}
565+
566+
.. versionadded:: 6.1
567+
568+
The ``#[Autowire]`` attribute was introduced in Symfony 6.1.
569+
570+
The ``#[Autowire]`` attribute can also be used for :ref:`parameters <service-parameters>`
571+
and even :doc:`complex expressions </service_container/expression_language>`::
572+
573+
// src/Service/MessageGenerator.php
574+
namespace App\Service;
575+
576+
use Psr\Log\LoggerInterface;
577+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
578+
579+
class MessageGenerator
580+
{
581+
public function __construct(
582+
// use the %...% syntax for parameters
583+
#[Autowire('%kernel.project_dir%/data')]
584+
string $dataDir,
585+
586+
#[Autowire('%kernel.debug%')]
587+
bool $debugMode,
588+
589+
// and @=... for expressions
590+
#[Autowire("@=service("App\\Mail\\MailerConfiguration").getMailerMethod()")]
591+
string $mailerMethod
592+
) {
593+
}
594+
// ...
595+
}
544596

545597
.. _autowiring-calls:
546598

0 commit comments

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