### 控制器
```
class UserController extends Controller
{
const USER_LOGIN = 'user_login'; //事件名称
public function __construct($id, $module, array $config = [])
{
parent::__construct($id, $module, $config);
$this->on(self::USER_LOGIN, ['app\models\User', 'notify']); //注册事件
}
public function actionIndex()
{
$user = User::findOne(['username' => 'admin']);
$event = new UserLoginEvent(); //实例化事件类
$event->user_id = $user->id; //对事件类进行赋值
$this->trigger(self::USER_LOGIN, $event); //触发事件
}
}
```
### 事件类
在app目录下创建event文件夹
```
<?php
namespace app\event;
use yii\base\Event;
class UserLoginEvent extends Event //继承此类
{
public $user_id = 0;
}
```
### 模型层
```
class User extends ActiveRecord implements IdentityInterface
{
public static function notify($event) //这里使用的是静态方法
{
print_r($event->user_id);
}
}
```
