CakePHP Container
An easy way to add a good dependency injection container to Cakephp 2 applications., (*1)
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., (*2)
Why?
I support a number of legacy CakePHP applications, and the lack of a container or a nice way to do DI was annoying., (*3)
Installation
Install with composer., (*4)
composer require burriko/cake-container
Load the plugin in Config/bootstrap.php. Bootstrap needs to be set to true., (*5)
CakePlugin::loadAll(['CakeContainer' => ['bootstrap' => 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:, (*6)
<?php
$container->addServiceProvider(new Jobsoc\ServiceProvider\DocumentManagementServiceProvider);
Check the League Container docs for more useful examples., (*7)
Change AppController
so that it implements League\Container\ContainerAwareInterface
and uses the League\Container\ContainerAwareTrait
trait., (*8)
class AppController extends Controller implements League\Container\ContainerAwareInterface
{
use League\Container\ContainerAwareTrait;
}
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., (*9)
$this->getContainer()->get(Jobsoc\DocumentSearch\DocumentManagement::class);
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., (*10)
<?php
App::uses('ContainerAwareShell', 'CakeContainer.Shell');
class AppShell extends 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., (*11)
ClassRegistry::getObject('container')->get('Jobsoc\DocumentSearch\DocumentManagement');