Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitbf8be7e

Browse files
committed
Move the autowire attribute sections
1 parentf32aec1 commitbf8be7e

File tree

3 files changed

+86
-145
lines changed

3 files changed

+86
-145
lines changed

‎controller.rst‎

Lines changed: 32 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -223,107 +223,46 @@ 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-
<containerxmlns="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-
<serviceid="App\Controller\LuckyController">
260-
<tagname="controller.service_arguments"/>
261-
<bindkey="$logger"
262-
type="service"
263-
id="monolog.logger.doctrine"
264-
/>
265-
<bindkey="$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.
286-
287-
For more information about services, see the:doc:`/service_container` article.
288-
289-
Autowire Parameter Attribute
290-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291-
292-
..versionadded::6.1
226+
..tip::
293227

294-
The ``#[Autowire]`` attribute was introduced in Symfony 6.1.
228+
If you need control over the *exact* value of an argument, you can use the
229+
``#[Autowire]`` attribute::
295230

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::
231+
// ...
232+
use Psr\Log\LoggerInterface;
233+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
234+
use Symfony\Component\HttpFoundation\Response;
299235

300-
use Psr\Log\LoggerInterface;
301-
use Symfony\Component\DependencyInjection\Attribute\Autowire;
302-
use Symfony\Component\HttpFoundation\Response;
303-
// ...
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+
}
304254

305-
/**
306-
* @Route("/lucky/number/{max}")
307-
*/
308-
public function number(
309-
int $max,
255+
You can read more about this attribute in:ref:`autowire-attribute`.
310256

311-
#[Autowire('@monolog.logger.request')]
312-
LoggerInterface $logger,
257+
..versionadded::6.1
313258

314-
#[Autowire('%kernel.project_dir%/data')]
315-
string $dataDir,
259+
The ``#[Autowire]`` attribute was introduced in Symfony 6.1.
316260

317-
#[Autowire('%kernel.debug%')]
318-
bool $debugMode,
261+
Like with all services, you can also use regular
262+
:ref:`constructor injection<services-constructor-injection>` in your
263+
controllers.
319264

320-
#[Autowire("@=service("App\\Mail\\MailerConfiguration").getMailerMethod()")]
321-
string $mailerMethod,
322-
): Response
323-
{
324-
$logger->info('We are logging!');
325-
// ...
326-
}
265+
For more information about services, see the:doc:`/service_container` article.
327266

328267
Generating Controllers
329268
----------------------

‎service_container.rst‎

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -687,56 +687,6 @@ 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-
740690
Binding Arguments by Name or Type
741691
---------------------------------
742692

‎service_container/autowiring.rst‎

Lines 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)

[8]ページ先頭

©2009-2025 Movatter.jp