2
votes

I have this controller:

use Doctrine\Common\Persistence\ManagerRegistry;

class AjaxEntityController
{
    protected $registry;

    public function __construct(ManagerRegistry $registry)
    {
        $this->registry = $registry;
    }

    ...
}

And this is my services.yml:

services:
    tapir_form.ajax_entity_controller:
        class: Tapir\FormBundle\Controller\AjaxEntityController
        arguments: [ '@doctrine' ]

But when I try to access this controller (by it's URL), I get this error:

Type error: Argument 1 passed to Tapir\FormBundle\Controller\AjaxEntityController::__construct() must implement interface Doctrine\Common\Persistence\ManagerRegistry, none given, called in /..../var/cache/dev/jms_diextra/controller_injectors/TapirFormBundleControllerAjaxEntityController.php on line 13

I've searched for this problem, and found lots of cases where the controller was not declared as a service or the arguments parameter was missing, but it's not my case.

I cleared the cache, added @Route(service="tapir_form.ajax_entity_controller") to my controller class, just in case. Of course I have my TapirFormExtension

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

If I remove the mandatory parameter from the constructor, the service works (I mean the controller action is called, no exception). This is the first time I use a controller as a service. Other services like form types are working with a similar configuration.

I'm using Symfony 2.7.3, PHP 5.6.13, Linux.

What else can be wrong?

Thanks in advance.

1
You need to change your routing information to specify the controller as a service: symfony.com/doc/current/cookbook/controller/… - Cerad
As @Cerad stated... You problem is that you probably defined route in routing.yml file, and later you defined service in @Route annotation.. - malcolm
That was it Cerad, thanks! I was using "Bundle:Controller:action" instead of "servicename:method" in my routing.yml. - Ernesto

1 Answers

0
votes

Since Symfony 3.3 you can use autowiring:

# app/services/services.yml
services:
    Tapir\FormBundle\Controller\AjaxEntityController:
        autowire: true

Or for more controllers, PSR-4 service autodiscovery:

# app/services/services.yml
services:
    _defaults:
        autowire: true

    Tapir\:
        resource: src/Tapir/{Controller}