控制器初始化中添加事件处理方法,在需要触发的地方直接触发
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 }