adamlundrigan / ldc-zf1-di-leaguecontainer
Basic controller dependency injection in ZF1 with League\Container
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-12-21 19:00:42 UTC
README
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?
-
Install the Composer package:
composer require adamlundrigan/ldc-zf1-di-league-container:1.*@stable
-
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() ); }
-
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. -
Profit! The injector will create a public property on the controller instance for each named service.