标签:
1、在app\components下新建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"; } }
'components' => [ 'mycomponent' => [ 'class' => 'app\components\MyComponent', ], ]
3、在controllers/TestController.php中用你的components
namespace app\controllers; use Yii; class TestController extends \yii\web\Controller { public function actionWelcome() { Yii::$app->mycomponent->welcome(); } }
标签:
原文地址:http://blog.csdn.net/xiaoyangxiaodong/article/details/45078303