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

[DependencyInjection] Add#[Target] documentation#17696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
OskarStark merged 1 commit intosymfony:5.4fromalexandre-daubois:targetattribute
Jan 10, 2023
Merged
Changes fromall 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 deletionsservice_container/autowiring.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp