adamlundrigan/ldc-zf1-di-leaguecontainer

Basic controller dependency injection in ZF1 with League\Container

1.1.0 2015-07-02 16:57 UTC

This package is not auto-updated.

Last update: 2024-12-21 19:00:42 UTC


README

Latest Stable Version License Build Status Code Coverage Scrutinizer Code Quality

What?

It's a simple glue layer which injects services registered in a League\Container into a ZF1 controller.

Heavily based on @weierophinney's now-many-moons-ago blog post "A Simple Resource Injector for ZF Action Controllers".

How?

  1. Install the Composer package:

    composer require adamlundrigan/ldc-zf1-di-league-container:1.*@stable
    
  2. Register the helper in your ZF1 application bootstrap:

    protected function _initContainer()
    {
        $container = new League\Container\Container();
        // Fill your container    
        return $container;
    }
    
    protected function _initContainerResourceInjector()
    {
        $this->bootstrap('container');
        
        Zend_Controller_Action_HelperBroker::addHelper(
            new \LdcZf1DiLeagueContainer\DependencyInjector()
        );
    }
    
  3. In each controller, define a list of dependencies to inject:

    class FooController extends Zend_Controller_Action
    {
        public $dependencies = array(
            'db',
            'layout',
            'navigation',
        );
    }
    

    If you use a non-numeric key for any value of $dependencies the injector will use that as the controller property to inject into.

  4. Profit! The injector will create a public property on the controller instance for each named service.