企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
### 控制器 ``` 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); } } ```