2017 © Pedro Peláez
 

cakephp-plugin cake-container

Adds League's DI Container to CakePHP 2.x

image

burriko/cake-container

Adds League's DI Container to CakePHP 2.x

  • Thursday, May 26, 2016
  • by burriko
  • Repository
  • 1 Watchers
  • 0 Stars
  • 480 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

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');

The Versions

26/05 2016

dev-master

9999999-dev https://github.com/burriko/cakephp-container

Adds League's DI Container to CakePHP 2.x

  Sources   Download

MIT

The Requires

 

container cakephp dependency injection

26/05 2016

1.0.1

1.0.1.0 https://github.com/burriko/cakephp-container

Adds League's DI Container to CakePHP 2.x

  Sources   Download

MIT

The Requires

 

container cakephp dependency injection

31/07 2015

1.0.0

1.0.0.0 https://github.com/burriko/cakephp-container

Adds League's DI Container to CakePHP 2.x

  Sources   Download

MIT

The Requires

 

container cakephp dependency injection