adamlundrigan/ldc-zf1-di-leaguecontainer

使用League\Container在ZF1中进行基本控制器依赖注入

1.1.0 2015-07-02 16:57 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:42:53 UTC


README

Latest Stable Version License Build Status Code Coverage Scrutinizer Code Quality

是什么?

这是一个简单的粘合层,它将注册在League\Container中的服务注入到ZF1控制器中。

主要基于@weierophinney多年前的一篇博客文章 "一个简单的ZF动作控制器资源注入器"。

如何实现?

  1. 安装Composer

    composer require adamlundrigan/ldc-zf1-di-league-container:1.*@stable
    
  2. 在ZF1应用的引导程序中注册辅助函数

    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. 在每个控制器中,定义要注入的依赖项列表

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

    如果你为$dependencies的任何值使用非数字键,注入器将使用该键作为要注入控制器实例的属性。

  4. 盈利!注入器将为每个命名的服务在控制器实例上创建一个公共属性。