0

I'm new in Symfony and after I finished course "Create your own PHP Framework" (which is based on symfony components), I wanted to expand my framework by inject constructor of controller by model, but I got stuck. I got following error.

Error:

Catchable fatal error: Argument 1 passed to Controller\HelloController::__construct() must be an instance of Model\TestModel, none given, called in /var/www/html/vendor/symfony/http-kernel/Controller/ControllerResolver.php on line 195 and defined in /var/www/html/src/Controller/HelloController.php on line 21

HelloController constructor

public function __construct(TestModel $testModel)
{
    $this->testModel = $testModel;
}

In container

....
$containerBuilder->register('model', \Model\TestModel::class);

$containerBuilder->register('hello', \Controller\HelloController::class)
->addArgument(new Reference('model'));   
....

Routes

$routes->add('index', new Routing\Route('/hello/{name}',array(
'name' => 'World',
'_controller' => 'Controller\HelloController::index'
)));

I will be grateful for every advice.

Thanks

Jakub Matczak
  • 15,341
  • 5
  • 46
  • 64
cagy
  • 21
  • 2
  • 1
    Pretty much a dup of https://stackoverflow.com/questions/54500765/controller-service-not-having-dependencies-injected-on-symfony-project#comment95812030_54500765. Basically, your controller resolver knows nothing about the container. – Cerad Feb 11 '19 at 00:34
  • You are right! Thanks – cagy Feb 11 '19 at 09:10

0 Answers0