Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions service_container/autowiring.rst
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,36 @@ If the argument is named ``$shoutyTransformer``,
But, you can also manually wire any *other* service by specifying the argument
under the arguments key.

Another possibility is to use the ``#[Target]`` attribute. By using this attribute
on the argument you want to autowire, you can define exactly which service to inject
by using its alias. Thanks to this, you're able to have multiple services implementing
the same interface and keep the argument name decorrelated of any implementation name
(like shown in the example above).

Let's say you defined the ``app.uppercase_transformer`` alias for the
``App\Util\UppercaseTransformer`` service. You would be able to use the ``#[Target]``
attribute like this::

// src/Service/MastodonClient.php
namespace App\Service;

use App\Util\TransformerInterface;
use Symfony\Component\DependencyInjection\Attribute\Target;

class MastodonClient
{
private $transformer;

public function __construct(#[Target('app.uppercase_transformer')] TransformerInterface $transformer)
{
$this->transformer = $transformer;
}
}

.. versionadded:: 5.3

The ``#[Target]`` attribute was introduced in Symfony 5.3.

Fixing Non-Autowireable Arguments
---------------------------------

Expand Down