### 控制器
```
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);
}
}
```