adamlundrigan / ldc-zf1-di-leaguecontainer
使用League\Container在ZF1中进行基本控制器依赖注入
1.1.0
2015-07-02 16:57 UTC
Requires
- league/container: ^1.3
Requires (Dev)
- fabpot/php-cs-fixer: ~1.0
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2024-09-14 17:42:53 UTC
README
是什么?
这是一个简单的粘合层,它将注册在League\Container中的服务注入到ZF1控制器中。
主要基于@weierophinney多年前的一篇博客文章 "一个简单的ZF动作控制器资源注入器"。
如何实现?
-
安装Composer包
composer require adamlundrigan/ldc-zf1-di-league-container:1.*@stable
-
在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() ); }
-
在每个控制器中,定义要注入的依赖项列表
class FooController extends Zend_Controller_Action { public $dependencies = array( 'db', 'layout', 'navigation', ); }
如果你为
$dependencies
的任何值使用非数字键,注入器将使用该键作为要注入控制器实例的属性。 -
盈利!注入器将为每个命名的服务在控制器实例上创建一个公共属性。