Integration dependency injection container Pimple in Yii framework

Integration dependency injection container Pimple in Yii framework

1) Install Pimple


  "require": {
    "petrgrishin/yiipimple": "dev-master"

2) Change the entry scripts

Just change this



Yii::createApplication('\YiiPimple\WebApplication', $config)->run();

3) Configuration

return array(
    // ...
    // dipendency injection configuration
    'container' => array(
        'class' => '\YiiPimple\CContainer',
        'services' => array(
            // ... put here your services
    // ...

4) Retrieve services

$service = Yii::app()->getContainer()->get('service');
// yii urlManager
$urlManager = Yii::app()->getContainer()->get('yii.core.urlManager');


  • PHP >=5.3.0
  • Pimple 1.0.*


Project on github.com

Total 2 comments

#18722 report it
marcovtwout at 2014/12/17 09:40am
Alternative implementation suggestion

Instead of replacing the web application or instead of using behaviors, you could simply convert it to a CApplicationComponent.

#13597 report it
yiqing95 at 2013/06/10 05:05am
just my opinion : don't think yii need injection container .

the CApplication self can be a container ! so i don't think we need others .

any way if you want that ,i think your doing is a invasion way . we often have our own class which extends the CWebApplication . so you can implements it as a CBehavior and config it in the main.php :

       array( 'ioc'=>  array('class'=>'MyDIConatiner',

the usage is still in same way .

Leave a comment

Please to leave your comment.

Create extension
No downloadable files yet