Advertisement

Yii2 事件--自定义事件和系统事件

阅读量:
复制代码
![](http://www.architecy.com/wp-content/uploads/2018/01/timg-300x142.jpg)
     
     
    
    
      
      首先撇开yii,让我们来理解一下js的事件
     
     
    
    
      
      以js的click事件为例--点击按钮时的弹出警告,首先要定义click事件,然后在点击的时候回触发事件,最后是弹出警告。
     
     
    
    
      
      事件就是这么一个过程。
     
     
    
    
      
      总结一下,事件一共分为三个过程
     
     
    
    
      
      1、定义事件
     
     
    
    
      
      2、触发事件
     
     
    
    
      
      3、处理事件
     
     
    
    
      
      当然,我们在写代码完成的时候,要按照312来进行。
     
     
    
    
      
      **一、首先使用yii来看一下自定义事件。**
     
     
    
    
      
      这里就以写操作的时候写日志做为一个例子吧
     
     
    
    
      
      1、处理事件--登录时记日志
     
     
    
    
      
      在模型中
     
     
    
    
      
      <?php
     
     
    
    
      
      namespace frontend\models;
     
     
    
    
      
      class WriteLog
     
     
    
    
      
      {
     
     
    
    
      
      public static function add(){
     
     
    
    
      
      echo '记录日志成功';
     
     
    
    
      
      }
     
     
    
    
      
      }
     
     
    
    
      
      2、在controller中定义事件
     
     
    
    
      
      <?php
     
     
    
    
      
      namespace frontend\controllers;
     
     
    
    
      
      class CountryController extends Controller
     
     
    
    
      
      {
     
     
    
    
      
      const EVENT_COUNTRY_SHOW = 'country_show';
     
     
    
    
      
      public function __construct($id, Module $module, array $config = [])
     
     
    
    
      
      {
     
     
    
    
      
      $this->on(self::EVENT_COUNTRY_SHOW,['frontend\models\WriteLog','add']);
     
     
    
    
      
      parent::__construct($id, $module, $config);
     
     
    
    
      
      }
     
     
    
    
      
      3、在country_show中触发事件
     
     
    
    
      
      <?php
     
     
    
    
      
      namespace frontend\controllers;
     
     
    
    
      
      class CountryController extends Controller
     
     
    
    
      
      {
     
     
    
    
      
      const EVENT_COUNTRY_SHOW = 'country_show';
     
     
    
    
      
      public function __construct($id, Module $module, array $config = [])
     
     
    
    
      
      {
     
     
    
    
      
      $this->on(self::EVENT_COUNTRY_SHOW,['frontend\models\WriteLog','add']);
     
     
    
    
      
      parent::__construct($id, $module, $config);
     
     
    
    
      
      }
     
     
    
    
      
      public function actionShow(){
     
     
    
    
      
      $this->trigger(self::EVENT_COUNTRY_SHOW);
     
     
    
    
      
      }
     
     
    
    
      
      4、还可以在事件中添加相关信息
     
     
    
    
      
      我们可以看到trigger有两个参数,第二个参数是来传递相关event事件的,类型也是event
     
     
    
    
      
      创建event类
     
     
    
    
      
      <?php
     
     
    
    
      
      namespace frontend\events;
     
     
    
    
      
      use yii\base\Event;
     
     
    
    
      
      class CountryShowEvent extends Event
     
     
    
    
      
      {
     
     
    
    
      
      public $ip = 0;
     
     
    
    
      
      }
     
     
    
    
      
      在controller中定义添加event
     
     
    
    
      
      <?php
     
     
    
    
      
      namespace frontend\controllers;
     
     
    
    
      
      class CountryController extends Controller
     
     
    
    
      
      {
     
     
    
    
      
      const EVENT_COUNTRY_SHOW = 'country_show';
     
     
    
    
      
      public function __construct($id, Module $module, array $config = [])
     
     
    
    
      
      {
     
     
    
    
      
      $this->on(self::EVENT_COUNTRY_SHOW,['frontend\models\WriteLog','add']);
     
     
    
    
      
      parent::__construct($id, $module, $config);
     
     
    
    
      
      }
     
     
    
    
      
      public function actionShow(){
     
     
    
    
      
      $event = new CountryShowEvent();
     
     
    
    
      
      $event->ip = \Yii::$app->request->getUserIP();
     
     
    
    
      
      $this->trigger(self::EVENT_COUNTRY_SHOW);
     
     
    
    
      
      }
     
     
    
    
      
      **二、yii2自带的事件处理**
     
     
    
    
      
      我测试的主要是在controller级的,应该会有多级控制
     
     
    
    
      
      在每个动作执行之后都会调用afterAction,这是触发事件
     
     
    
    
      
      其他都不变,主要是在controller中修改
     
     
    
    
      
      <?php
     
     
    
    
      
      namespace frontend\controllers;
     
     
    
    
      
      class CountryController extends Controller
     
     
    
    
      
      {
     
     
    
    
      
      public $layout = 'main1';
     
     
    
    
      
      const EVENT_COUNTRY_SHOW = 'country_show';
     
     
    
    
      
      public function __construct($id, $module, array $config = [])
     
     
    
    
      
      {
     
     
    
    
      
      $this->on(self::EVENT_COUNTRY_SHOW,['frontend\models\WriteLog','add']);
     
     
    
    
      
      parent::__construct($id, $module, $config);
     
     
    
    
      
      }
     
     
    
    
      
      public function actionShow(){
     
     
    
    
      
      echo 'show test';
     
     
    
    
      
      }
     
     
    
    
      
      public function afterAction($action, $result)
     
     
    
    
      
      {
     
     
    
    
      
      $event = new CountryShowEvent();
     
     
    
    
      
      $event->ip = \Yii::$app->request->getUserIP();
     
     
    
    
      
      $this->trigger(self::EVENT_COUNTRY_SHOW,$event);
     
     
    
    
      
      }
     
     
    
      
      
    <http://www.architecy.com/archives/419>

全部评论 (0)

还没有任何评论哟~