Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in bundled controller when not autowiring

Tags:

symfony

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?

like image 904
justinvoelker Avatar asked May 31 '19 22:05

justinvoelker


1 Answers

Based on the deprecation, I assume your controller is extending AbstractController.

There are two possible approaches to solve this deprecation:

  1. 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.

  2. 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 {}
like image 177
Arno Hilke Avatar answered Oct 13 '22 12:10

Arno Hilke