emfi
emfi

Reputation: 659

Symfony: Autowiring does not work, debug:autowiring does not list own classes

I am using Symfony 4.2

Here's my config for my classes i want to autowire:

services:
    _defaults:
        autowire: true
#        autoconfigure: true
    Cyrene\components\:
        resource: '../../app/components/*'
        tags: ['controller.service_arguments']
        exclude:
            - '../../app/components/common/*'

The paths are correct, because using the debug:container command of symfony, it does show me all the classes i have in the "components"-folder.

But using the debug:autowiring command of symfony, it does not show any of my classes, even though they have the tag controller.service_arguments, which should make them autowireable.

what i am doing wrong here?

I am facing another problem, too...

although i am using one autowireable class, e.g., Psr\Log\LoggerInterface (which is autowireable from symfony by default), symfony does not pass it to the constructor:

use Cyrene\core\actions\AbstractAdminAction;
use Psr\Log\LoggerInterface;

class OverviewIndexAction extends AbstractAdminAction
{
    public function __construct(LoggerInterface $logger, OverviewIndexResponder $responder)
    {
        $this->responder = $responder;
    }

I get this error message: [...] Too few arguments to function Cyrene\components\admin\application\overview\index\OverviewIndexAction::__construct(), 0 passed [...] but exactly 2 expected [...]

that leads me to the assumption, that autowiring generally doesn't work.

the $logger should have been autowired, because it is listed with the debug:autowiring command of symfony. OverviewIndexResponder $responder is within the same path/namespace as OverviewIndexAction, hence no use.

Upvotes: 1

Views: 1687

Answers (2)

Sebastian Skurnóg
Sebastian Skurnóg

Reputation: 161

If you are using Symfony 4.2.0 - there is a bug in this version.

To fix that try:

composer require phpdocumentor/reflection-docblock

More about bug: https://github.com/symfony/symfony/issues/29442

Upvotes: 1

emfi
emfi

Reputation: 659

it's funny, oftenly you find the issue, when you ask someone else.

The issue was, that my own ControllerResolver did not extend the correct superclass.

This is correct:

use Symfony\Component\HttpKernel\Controller\ContainerControllerResolver as SymfonyControllerResolver;

class ControllerResolver extends SymfonyControllerResolver

The ContainerControllerResolver takes care of injecting the correct objects.

although autowiring works, debug:autowiring still doesn't list my classes.

Upvotes: 0

Related Questions