File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change @@ -529,6 +529,32 @@ If the argument is named ``$shoutyTransformer``,
529
529
But, you can also manually wire any *other * service by specifying the argument
530
530
under the arguments key.
531
531
532
+ Another possibility is to use the ``#[Target] `` attribute. By using this attribute
533
+ on the argument you want to autowire, you can define exactly which service to inject
534
+ by using its alias. Thanks to this, you're able to have multiple services implementing
535
+ the same interface and keep the argument name decorrelated of any implementation name
536
+ (like shown in the example above).
537
+
538
+ Let's say you defined the ``app.uppercase_transformer `` alias for the
539
+ ``App\Util\UppercaseTransformer `` service. You would be able to use the ``#[Target] ``
540
+ attribute like this::
541
+
542
+ // src/Service/MastodonClient.php
543
+ namespace App\Service;
544
+
545
+ use App\Util\TransformerInterface;
546
+ use Symfony\Component\DependencyInjection\Attribute\Target;
547
+
548
+ class MastodonClient
549
+ {
550
+ private $transformer;
551
+
552
+ public function __construct(#[Target('app.uppercase_transformer')] TransformerInterface $transformer)
553
+ {
554
+ $this->transformer = $transformer;
555
+ }
556
+ }
557
+
532
558
.. _autowire-attribute :
533
559
534
560
Fixing Non-Autowireable Arguments
You can’t perform that action at this time.
0 commit comments