Integration dependency injection container Pimple in Yii framework ¶
1) Install Pimple
composer.json:
[javascript]
{
"require": {
"petrgrishin/yiipimple": "dev-master"
}
}
2) Change the entry scripts
Just change this
Yii::createWebApplication($config)->run();
into
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');
Require ¶
- PHP >=5.3.0
- Pimple 1.0.*
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 :
'behaviors'=> array( 'ioc'=> array('class'=>'MyDIConatiner', 'services'=>array( ....
the usage is still in same way .
Alternative implementation suggestion
Instead of replacing the web application or instead of using behaviors, you could simply convert it to a CApplicationComponent.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.