-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Cannot autowire service (...) when using #[Target]
attribute in Symfony 6.3
#50541
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
|
Try running #[Target('consoleLogger')] private LoggerInterface $logger It looks like an invalid target name was ignored in previous versions Symfony and the default logger was used. |
Actually no, it takes the alias of the parameter name, not the service id:
|
Hum, well... It looks like our If I debug the service symfony console debug:container monolog.logger.console
Information for Service "monolog.logger.console"
================================================
---------------- -------------------------------------------
Option Value
---------------- -------------------------------------------
Service ID monolog.logger.console
Class Symfony\Bridge\Monolog\Logger
Tags -
Calls pushProcessor, pushProcessor, pushHandler
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired no
Autoconfigured no
Usages Psr\Log\LoggerInterface $consoleLogger
console.error_listener
---------------- -------------------------------------------
! [NOTE] The "monolog.logger.console" service or alias has been removed or inlined when the container was compiled. But if I switch to symfony console debug:container monolog.logger.console
Information for Service "monolog.logger.console"
================================================
---------------- ---------------------------------------------
Option Value
---------------- ---------------------------------------------
Service ID monolog.logger.console
Class Symfony\Bridge\Monolog\Logger
Tags -
Calls pushProcessor, pushProcessor, pushHandler
Public no
Synthetic no
Lazy no
Shared yes
Abstract no
Autowired no
Autoconfigured no
Usages Psr\Log\LoggerInterface $consoleLogger
App\Command\Website\GenerateSitemapsCommand
console.error_listener
---------------- ---------------------------------------------
! [NOTE] The "monolog.logger.console" service or alias has been removed or inlined when the container was compiled. Thanks! |
…(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.0
Description
Hi,
While upgrading our application from Symfony 6.2.x to 6.3, I faced an issue about when using the
#[Target]
attribute in a Command's constructor.With the following code:
I have the following error with Symfony 6.3 (it works fine with Symdony 6.2):
Note that the Monolog handler
console
is properly defined.Thanks!
How to reproduce
See https://github.com/Kocal/sf6.3-reproducer-autowiring-target-attribute for reproduction repository and steps.
Possible Solution
No response
Additional Context
No response
The text was updated successfully, but these errors were encountered: