Pascal's Blog

An example of how to use Pimple DI with ZF 1.x

via Blog on Thu 15 Mar 2012

After having had a look at Silex, and struggling with the somewhat cumbersome ini configurations and YADIF, I wanted to try out whether I could use Pimple as DI container for ZF 1.x

Turns out I can, as you can just select Pimple to be the bootstrap container:

$c = new Pimple();
$application->getBootstrap()->setContainer($c);

Of course you can subclass that, in order to make it re-usable:

// index.php
$c = new DITest\Container();
$application->getBootstrap()->setContainer($c);

// in library/DITest/Container.php
namespace DITest;
use Pimple;

class Container extends Pimple
{
    public function __construct()
    {
        $this['foo'] = $this->share(function($this) {
            $c = new \stdClass;
            $c->bar = function() {
                echo 'bar';
            };

            return $c;
        });
    }
}

Et voila, now we can access the container via the getInvokeArg method:

$this->c = $this->getInvokeArg('bootstrap')->getContainer();
$this->c['foo']->bar();

Get the example app as gist