Passing paramerter to constructor in controlloer


I would like to pass parameter to constructor in controller. Is it possible to do ?

I am trying to pass interface defination in constructor.

or is it possible to bind constructor in DI ?


    use Phalcon\Repositories\IUsersRepository;

    class UsersController extends ControllerBase
        private  $users;

        public function __construct(IUsersRepository $usersRepository)
            $this->users = $usersRepository;

edited Dec '14

Could you use onConstruct() and pickup your dependency from the DI.

Something like :

    public function onConstruct()
        $this->users = $this->getDI()->get('userRepository');

edited Dec '14

Thanks for reply.

How can I set interface userRepository in DI ?

Thanks Shivanshu

edited Dec '14

You could create your own AbstractController that extends Injectable

namespace MyNs\Controller;
use Phalcon\DI\Injectable;
abstract class AbstractController extends Injectable {}
namespace MyNs\Controller;
use MyNs\Repository\UserRepoInterface;
class UserController extends AbstractController {
    private $userRepo;
    public function __construct(UserRepoInterface $repo) {
        $this->userRepo = $repo;
$controller = $di->get('MyNs\Controller\UserController', [
    'arguments' => [
        ['type' => 'service', 'value' => 'MyNs\Repository\UserRepo']

Disclaimer: I dont recall if the arguments definition is 100% correct, the value key might actually be "name" or something.

As a side-note, I dont think the dispatcher actually does any interface check, so you are free to not use it as your super class. It just checks whether your controller has a method onConstruct() I think, and whether it implements the events and DI interfaces.

*I think

Thanks Brandon. Agree with you. Will try this . cheers

Still I am not able to pass variable. Argument 1 passed to UsersController::__construct() must be an instance of Phalcon\Repositories\IUsersRepository, none given in


I fixed by passing below code in service.php file. Thanks all for your help.

$di->set('usersRepository', array(
    'className' => 'Phalcon\Repositories\UsersRepository'

$di->set('UsersController', array(
    'className' => 'UsersController',
    'arguments' => array(
        array('type' => 'service', 'name' => 'usersRepository')