简体   繁体   中英

Define dependencies for framework controllers with Pimple

So I have a controller which I have added its dependencies with Pimple like this:

$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:

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'] . Try assigning the controller to Menu_builder before instantiating Account class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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