码迷,mamicode.com
首页 > 其他好文 > 详细

在YII中如何新添加一个component

时间:2015-06-30 20:18:38      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

Step1:

Make a folder named "components" in your project root folder.

step2:

write your custom component inside components folder eg:MyComponent.php

namespace app\components;
  
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
 
class MyComponent extends Component
{
 public function welcome()
 {
  echo "Hello..Welcome to MyComponent";
 }
 
}

Step3:

Add your component inside the config/web.php file

‘components‘ => [
 
         ‘mycomponent‘ => [
 
            ‘class‘ => ‘app\components\MyComponent‘,
 
                ],
           ]

Step4:

You are done!. Now you can use your component method "welcome" inside any of your app controller actions. eg:controllers/TestController.php

namespace app\controllers;
use Yii;
 
class TestController extends \yii\web\Controller
{
    public function actionWelcome()
    {
       Yii::$app->mycomponent->welcome();
    }
 
}

 

在YII中如何新添加一个component

标签:

原文地址:http://www.cnblogs.com/wlemory/p/4611422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!