-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Attribute #[Target()] does not work #54578
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think your service may not be defined as an alias but as a service.. Maybe try using "alias" instead of "class" in your services.yaml ? https://symfony.com/doc/current/service_container/alias_private.html#aliasing |
Hello @smnandre , I changed a little the service.yaml file as you advised and I also uncomment this line app.message.message_first: '@App\Service\MessageFirst'
app.message.message_second: '@App\Service\MessageSecond'
# Default used service
App\Interface\MessageInterface: '@app.message.message_first'
# Called service if we use the $notification variable (put here to compare with the Target attribute feature)
App\Interface\MessageInterface $notification: '@app.message.message_second'
App\Service\NotificationSender:
autowire: true In the NotificationSender class, I changed the variable for the parameter type Without the Target attribute feature in the NotificationSender, everything works well. The two services are taking into account depending on commenting/uncommenting this line So, our change in the services.yaml is OK. Now, I test the Target attribute feature: First case: Now, I suppose I comment this line in the yaml file: When I add the Target attribute in the NoficationSender class with the first service, it still doesn't work: class NotificationSender
{
public function __construct(
#[Target('app.message.message_first')]
private MessageInterface $notification
){
}
public function send(string $word): string
{
$sentence = $this->notification->show($word);
return $sentence;
}
} I have this error:
When I try the second service with:
Second case: I uncomment this line in the yaml file:
I tested the second service with
Regards, |
Try with |
I tested |
The In your case, that would look something like this: services:
App\Interface\MessageInterface $firstMessage: '@App\Service\MessageFirst'
App\Interface\MessageInterface $secondMessage: '@App\Service\MessageSecond' public function __construct(
#[Target('secondMessage')]
private MessageInterface $message
) {
} |
@HypeMC Thank you very much. Now, it works! |
…(HypeMC) This PR was merged into the 5.4 branch. Discussion ---------- [DependencyInjection] Clarify the `#[Target]` attribute The `#[Target]` attribute seems to be a constant source of confusion for developers, as evident by: - symfony/symfony#50541 - symfony/symfony#51565 - symfony/symfony#54578 Also, the example given is either unclear or just wrong. Hopefully, this helps clarify things. Commits ------- 2fb1ada [DependencyInjection] Clarify the `#[Target]` attribute
Symfony version(s) affected
6.4.6
Description
Hello,
When I tested the
#[Target]
attribute feature that you can see in the docs here: Target attribute docs, it does not work.Thanks in advance,
How to reproduce
I created a project which contains:
Target
attribute featureHere the link of the project: bug_app You will find some comments explaining the bug in the NotificationSender service.
Possible Solution
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: