简体   繁体   English

使用 Pimple 定义框架控制器的依赖项

[英]Define dependencies for framework controllers with Pimple

So I have a controller which I have added its dependencies with Pimple like this:所以我有一个控制器,我在 Pimple 中添加了它的依赖项,如下所示:

$this->container['Account'] = $this->container->factory(function ($c) {
    return new Account(
        $c['Menu_builder']
    );
});

And when I go to the URL of any action in this controller it just says:当我转到此控制器中任何操作的 URL 时,它只会说:

Message: Argument 1 passed to Account::__construct() must be an instance of Menu_builder, none given, called in website/system/core/CodeIgniter.php on line 482 and defined Filename: controllers/Account.php Line Number: 13

To load any class with dependencies I usually say:要加载任何具有依赖项的类,我通常会说:

$account = $this->container['Account'];

But Im not sure where to put this call in the case of a framework controller.但是我不确定在框架控制器的情况下把这个调用放在哪里。

The controller looks like this:控制器看起来像这样:

class Account extends MY_Controller
{
    private $menu_builder;

    public function __construct(Menu_builder $menu_builder){
        $this->menu_builder = $menu_builder;
    }
    // ...
}

QUESTION: What am I doing wrong here?问题:我在这里做错了什么? The above works fine for returning any classes except for controllers.以上适用于返回除控制器以外的任何类。

You're probably not assigning a value to $c['Menu_builder'] before executing $account = $this->container['Account'] .在执行$account = $this->container['Account']之前,您可能没有为$c['Menu_builder'] Try assigning the controller to Menu_builder before instantiating Account class.在实例化 Account 类之前尝试将控制器分配给 Menu_builder。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM