We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

constructor injection problem

I use constructor injection in my Controller:

     protected $_usersRepository;
     public function onConstruct(UsersRepository $repository)
     {
         $this->_usersRepository = $repository;
     }

     public function indexAction()
    {
         $this->_usersRepository->create();
     }

and register DI user repository

$di->set('userRepository', [
    'className' => 'Hyenas\Repositories\UsersRepository'
]);
$di->set('indexController', [
    'className' => 'Hyenas\Backend\Controllers\IndexController',
    'arguments' => [
        ['type' => 'service', 'name' => 'userRepository'],
    ]
]);

but i has error Catchable fatal error: Argument 1 passed to Hyenas\Backend\Controllers\IndexController::onConstruct() must be an instance of Hyenas\Repositories\UsersRepository, none given in /vagrant/first-app/apps/modules/backend/controllers/IndexController.php on line 11

I use document here https://docs.phalcon.io/en/latest/reference/di.html#constructor-injection how can i solve that's, I'm newbie working phalcon, please help me thank you.

Parameters passed to the constructor aren't passed to onConstruct, you can better use injection via setters: https://docs.phalcon.io/en/latest/reference/di.html#setter-injection

edited Apr '15

Parameters passed to the constructor aren't passed to onConstruct, you can better use injection via setters: https://docs.phalcon.io/en/latest/reference/di.html#setter-injection

sorry but not solve my error :(

Catchable fatal error: Argument 1 passed to Hyenas\Backend\Controllers\IndexController::onConstruct() must be an instance of Hyenas\Repositories\UsersRepository, none given in /vagrant/first-app/apps/modules/backend/controllers/IndexController.php on line 11

$di->set('userRepository', [
    'className' => 'Hyenas\Repositories\UsersRepository'
]);

$di->set('indexController', [
    'className' => 'Hyenas\Backend\Controllers\IndexController',
    'calls'     => [
        [
            'method'    => 'onConstruct',
            'arguments' => [
                ['type' => 'service', 'name' => 'userRepository']
            ]
        ]
    ]
]);

p/s: i use phalcon 2.0

DI does not pass the argument to onConstruct, because onConstruct != __construct, that's why you're getting an error