-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Target parameter attribute does not work for Services #51565
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
The |
closing as explained |
Thank you @HypeMC It works ! I added a However I should mention that in the doc, the statement
could be confusing as there is no example explaining the idea. Thank you again for the explanation Best regards ! |
the Target attribute works with named aliases, allowing to keep the argument name itself whather you want (so not always matching your named alias) |
did you add this in your bind configs? I do not know why the servers are not found in binds :/ |
Hi @eerison No i didn't have to add any bind config. You need to have a named alias and then you can report the name as Target's argument |
…(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.3
Description
Hello,
I am having an issue with the
#[Target]
attribute when using it in services.According to https://symfony.com/doc/current/service_container/autowiring.html#dealing-with-multiple-implementations-of-the-same-type,
when I have 2 services implementing the same interface i have 2 choices:
create an alias from the Interface and use a default service and create a named alias for the other services
use the Target attribute.
With the first choice, everything works fine. But with the 2nd choice, I have created a default alias from the interface, then I create another alias to the other service. When I use the alias in some service I got an error
Cannot resolve argument $client of "App\Controller\TwitterController::index()": Cannot autowire service "App\Service\TwitterClient": "#[Target('appTransformerUpper')" on argument "$transformer" of method "__construct()"
Am I doing something wrong ?
How to reproduce
And then, create another alias to the other service (cf the doc)
Use the Interface as a Type-hint in some service (eg. TwitterClient service) like the following
Possible Solution
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: