发现
Yii2有afterLogin和beforLogin两个方法(事件),但是不知道怎么才能使用。
在User模型我已经写了如下代码:
phppublic function behaviors() { return [ 'timestamp' => [ 'class' => TimestampBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'activated_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at' ], ] ]; }
因为ActiveRecord里只有数据库增删改查的事件,把yii\web\User::EVENT_AFTER_LOGIN加在这儿也没用。。。
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
首先明确两点
* 你的User Model是继承自 ActiveRecord
* afterLogin 和 beforeLogin 是 yii\web\User 的两个事件
那么你把 yii\web\User 的两个事件挂载在 User Model 肯定不会去触发了。
那么就可以通过配置来解决,我们知道,配置是支持事件挂载的;
例如: