Yii Service Locator Dependency Injection

Source: Internet
Author: User

<?PHP/** * Created by Phpstorm. * DATE:2016/5/25 * time:18:33 * Service locator Dependency Injection*/namespace Frontend\controllers; UseYii; UseYii\web\controller; UseYii\di\container; UseYii\di\servicelocator;classDependencyinjectservicecontrollerextendscontroller{ Public functionActionindex () {Yii::$container->set (' Frontend\controllers\driver ', ' frontend\controllers\mandriver '); $SL=NewServicelocator (); $SL->set (' Car ',[            ' Class ' = ' Frontend\controllers\car ',        ]); $car=$SL->get (' Car '); $car-run (); /*' car ' =>[' frontend\controllers\driver ', ' frontend\controllers\mandriver '; can be configured in config components at this time Yii:: $app->car->run ();*/        /*Plate 2 * Yii:: $container->set (' frontend\controllers\driver ', ' frontend\controllers\mandriver ');        Yii:: $app->car->run (); */    }}Interfacedriver{ Public functionDrive ();}classMandriverImplementsdriver{ Public functionDrive () {Echo"I am an old man! "; }}classcar{Private $driver=NULL;  Public function__construct (Driver$driver)//Line 20th implements interface delivery, eliminating strong dependencies    {        $this->driver =$driver; }     Public functionrun () {$this->driver->Drive (); }}

config.php

<?PHP$config= [    ' Components ' = [//Application Components' Request ' = [            //!!! Insert a secret key in the following (if it was empty)-this is required by cookie validation' Cookievalidationkey ' = ' fvfpjzskydscnsroxvd8f8att6cy0rvj ',        ], ' car ' =[            ' Class ' = ' Frontend\controllers\car '        ],    ],];if(!yii_env_test) {    //configuration adjustments for ' Dev ' environment    $config[' Bootstrap '] [] = ' Debug '; $config[' Modules '] [' Debug '] = [        ' Class ' = ' Yii\debug\module ',    ]; $config[' Bootstrap '] [] = ' GII '; $config[' Modules '] [' gii '] = [        ' Class ' = ' Yii\gii\module ',    ];}return $config;

Yii Service Locator Dependency Injection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.