控制器初始化中添加事件处理方法,在需要触发的地方直接触发

 1     public function init()
 2     {
 3         parent::init(); // TODO: Change the autogenerated stub
 4         Yii::$app->on('yii2event',function($event){
 5             $args=$event->sender;
 6             echo $args['name'];
 7             echo "<br/>";
 8             Yii::$app->off('yii2event');
 9         });
10     }
11     public function actionYii2event(){
12         Yii::$app->trigger('yii2event',new Event(['sender'=>['age'=>18,'sex'=>'mail','name'=>'Hello world']]));
13     }

 Yii中事件触发机制

相关文章:

  • 2021-12-28
  • 2021-04-11
  • 2021-05-01
  • 2021-12-28
  • 2022-12-23
  • 2021-12-06
  • 2022-12-23
  • 2021-04-04
猜你喜欢
  • 2021-06-09
  • 2021-07-23
  • 2021-11-30
  • 2021-10-02
  • 2021-09-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案