[moka同学笔记]Yii2 自定义class、自定义全局函数(摘录)

1、在appcomponents下新建MyComponent.PHP

    namespace appcomponents;  
       
       
    use Yii;  
    use yiiaseComponent;  
    use yiiaseInvalidConfigException;  
       
    class MyComponent extends Component  
    {  
     public function welcome()  
     {  
      echo "Hello..Welcome to MyComponent";  
     }  
       
    }


2、在config/web.php加入你的components

'components' => [  
   
         'mycomponent' => [  
   
            'class' => 'appcomponentsMyComponent',  
   
            ],  
           ] 

3、在controllers/TestController.php中用你的components

namespace appcontrollers;  
use Yii;  
   
class TestController extends yiiwebController  
{  
    public function actionWelcome()  
    {  
       Yii::$app->mycomponent->welcome();  
    }  
   
} 

原文地址:http://blog.csdn.net/xiaoyangxiaodong/article/details/45078303

我生活的地方,我为何要生活。
原文地址:https://www.cnblogs.com/hsd1727728211/p/5979513.html