Download the PHP package burriko/cake-container without Composer
On this page you can find all versions of the php package burriko/cake-container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download burriko/cake-container
More information about burriko/cake-container
Files in burriko/cake-container
Package cake-container
Short Description Adds League's DI Container to CakePHP 2.x
License MIT
Homepage https://github.com/burriko/cakephp-container
Informations about the package cake-container
CakePHP Container
An easy way to add a good dependency injection container to Cakephp 2 applications.
This installs League's Container, injects it into controllers, and adds it into CakeRegistry so you can access it as a singleton from anywhere else you may need it.
Why?
I support a number of legacy CakePHP applications, and the lack of a container or a nice way to do DI was annoying.
Installation
Install with composer.
Load the plugin in Config/bootstrap.php. Bootstrap needs to be set to true.
Usage
Create a file at Config/container.php to contain your container config. In here you can specify anything from the League Container docs. For example, to load a service provider your config file would contain something like:
Check the League Container docs for more useful examples.
Change AppController
so that it implements League\Container\ContainerAwareInterface
and uses the League\Container\ContainerAwareTrait
trait.
Usage within Controllers
You can access the container from within a controller by calling $this->getContainer()
. So to get an instance of a class named DocumentManagement that was set up by your service provider you could do this in your controller.
Usage within Shells
You can access the container in a shell the same way you'd use it in a controller by extending ContainerAwareShell. The easiest way to do this is to change AppShell to extend ContainerAwareShell.
Usage from other places
If you really need to use the container outside of a controller you can get an instance of it from the ClassRegistry.