The argument passed to the controller must be an instance of ContainerInterface, an instance of appDevDebugProjectContainer

Why do I have this error?

Catchable Fatal Error: argument 1 passed to Application \ Sonata \ ProductBundle \ Controller \ ProductAdminController :: __ construct () must be an instance of ContainerInterface, an instance of appDevDebugProjectContainer specified

Here are my services.yml:

services:
    product_admin_controller:
      class: Application\Sonata\ProductBundle\Controller\ProductAdminController
      arguments: ["@service_container"]
      tags:
            - { name: doctrine.event_listener, event: postLoad, connection: default  }

And my controller:

class ProductAdminController extends Controller
{
    protected $container;

    public function __construct(\ContainerInterface $container)
    {
        $this->container = $container;
    }
}
+4
source share
2 answers

You should enter the container using the "calls" option, and not as an argument, which I think:

services:
    product_admin_controller:
      class: Application\Sonata\ProductBundle\Controller\ProductAdminController
      arguments: ["@another_service_you_need"]
      tags:
            - { name: doctrine.event_listener, event: postLoad, connection: default  }
      calls:
            -   [ setContainer,["@service_container"] ]

Also, be sure to create the public "setContainer ()" method in your listener class.

+3
source

-, __constract()? setContainer(), ContainerInterface $ , :

<?php
...
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;

...
class YourClass extends Controller
{

    public function setContainer(ContainerInterface $container = null)
    {
        // your stuff
    }
}

: , ? $this- > get ('{service_id}' .

+1

All Articles