1
votes

I'm using symfony 3.1.7 and I have problems with inject services into a controller, my english is bad I believe that it is better if I show all the code.

The error:

Type error: Argument 1 passed to ...Bundle\Controller\AdminController::__construct() must implement interface Symfony\Component\DependencyInjection\ContainerInterface, none given, called in /......./project/var/cache/dev/classes.php on line 2512

This is my code:

GenericRestController

namespace MyCoreBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use MyCoreBundle\Handler\GenericRestHandlerInterface as Generic;
/** other uses like FOSRestController **/

/**
 * Class GenericRestController
 */
abstract class GenericRestController extends FOSRestController
{
  protected $handler;
  protected $container;

  public function __construct(Container $container,Generic $handler)
  {
      $this->container = $container;
      $this->handler = $handler;
  }

  /** other methods like get, post, put, etc **/
}

AdminController

namespace ...Bundle\Controller;
use MyCoreBundle\Controller\GenericRestController;

class AdminController extends GenericRestController
{

}  

RESOLVED

The solution was change the routes as Cerad says.

Oficial documentation

services.yml

services:
    app.hello_controller:
        class: AppBundle\Controller\HelloController

routing.yml

hello:
    path:     /hello
    defaults: { _controller: app.hello_controller:indexAction }

Thanks mtaqi and Cerad

1
did u see what php app/console container:debug lists shows? - Muhammad Taqi
Need to adjust your route to use the controller as a service. As it stands right now, the controller is being instantiated by the controller resolver using it's class name and not the container. Check the docs defining controllers as services carefully. And when you get it running, remove the container from the constructor and adjust your service definition to call setContainer. Things will be a bit cleaner. - Cerad
You are right @Cerad !! thank both for the help!! - Julieta
Please don't edit post to inject response. Put your answer below and accept it. Otherwise, some people like me spent time to read your resolved problem ;-) - Alexandre Tranchant

1 Answers

0
votes

The solution was change the routes as Cerad says.

Oficial documentation

services.yml

services:
    app.hello_controller:
    class: AppBundle\Controller\HelloController

routing.yml

hello:
   path:     /hello
   defaults: { _controller: app.hello_controller:indexAction }

Sorry for the delay and thanks!