Symfony documentation states that public bundles should explicitly configure their services and not rely on autowiring. As such, I am using the below to configure a Controller service in my bundle.
<service id="blah\blah\SecurityController">
<argument type="service" id="security.authentication_utils"/>
<tag name="controller.service_arguments"/>
<tag name="container.service_subscriber"/>
</service>
The security argument is because I am using authenticationUtils->getLastAuthenticationError() within a login method.
However, such a service definition produces a deprecation error. User Deprecated: Auto-injection of the container for "blah\blah\SecurityController" is deprecated since Symfony 4.2. Configure it as a service instead. at /var/www/html/vendor/symfony/framework-bundle/Controller/ControllerResolver.php:64)
If I simply add autowire="true"
to the service definition above, the error goes away (at which point I don't need that existing argument either). However, I would like to follow Symfony recommendations about explicit configuration.
What am I missing that is being auto-injected when autowire is set to true that I need to explicitly include?
Based on the deprecation, I assume your controller is extending AbstractController.
There are two possible approaches to solve this deprecation:
If you are using functionality from AbstractController not at all or only barely, you can just not extend anything at all. If you use e.g. only one function from AbstractController, you might choose to re-implement it to reduce your dependencies. This is also mentioned in the official docs.
If you want to keep extending AbstractController, you need to inject the container manually. E.g. like this:
services.xml
<service id="App\Controller\FooController">
<call method="setContainer">
<argument type="service" id="service_container"/>
</call>
</service>
FooController.php
class FooController extends AbstractController {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With