构造函数中的Phalcon php pass参数

构造函数中的Phalcon php pass参数,php,phalcon,Php,Phalcon,我想将参数传递给控制器中的构造函数。有可能吗 我试图在构造函数中传递接口定义 或者可以在DI中绑定或设置构造函数吗 下面是我的代码 <?php use Phalcon\Repositories\IUsersRepository; class UsersController extends ControllerBase { private $users; public function __construct(IUsersRepository $usersReposi

我想将参数传递给控制器中的构造函数。有可能吗

我试图在构造函数中传递接口定义

或者可以在DI中绑定或设置构造函数吗

下面是我的代码

<?php

use Phalcon\Repositories\IUsersRepository;

class UsersController extends ControllerBase
{
    private  $users;

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

是的,你可以。。看看

http://docs.phalconphp.com/en/latest/reference/di.html#instantiating-classes-via-the-service-container
如果您想在每次请求时发送数据,请使用
dispatch Service

$di->set('IndexController', function() {
    $component = new Component();
    $component->private_method();
    return $component;
}, true);

我想知道你是否需要这种方法

我已通过在service.php中使用以下代码修复了此问题

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

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

我想在phalcon中使用存储库模式。所以尝试在构造函数中传递存储库定义