Yii2 has two methods (events): afterLogin and beforLogin, but I don't know how to use them. In the User model, I have written the following code: {code...} Because ActiveRecord only contains the database addition, deletion, modification, and query events, add yiiwebUser: EVENT_AFTER_LOGIN...
FoundYii2
YesafterLogin
AndbeforLogin
Two methods (events), but do not know how to use them.
InUser Model
I have written the following code:
php
public function behaviors() { return [ 'timestamp' => [ 'class' => TimestampBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'activated_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at' ], ] ]; }
BecauseActiveRecord
Only add, delete, modify, and query database events.yii\web\User::EVENT_AFTER_LOGIN
It's useless to add it here...
Reply content:
FoundYii2
YesafterLogin
AndbeforLogin
Two methods (events), but do not know how to use them.
InUser Model
I have written the following code:
php
public function behaviors() { return [ 'timestamp' => [ 'class' => TimestampBehavior::className(), 'attributes' => [ ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'activated_at', 'updated_at'], ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at' ], ] ]; }
BecauseActiveRecord
Only add, delete, modify, and query database events.yii\web\User::EVENT_AFTER_LOGIN
It's useless to add it here...
First, clarify two points
* Your User Model is inherited from ActiveRecord
* AfterLogin and beforeLogin are two events of yii \ web \ User.
If you mount two yii \ web \ User events to the User Model, they will not be triggered.
We can solve this problem through configuration. We know that the configuration supports event mounting;
For example:
'Components' => [... 'user' => ['identityclass' => 'common \ models \ user', 'enableautologin' => true, 'on beforelogin' => function ($ event) {$ user = $ event-> identity; // here, the User Model instance $ user-> last_login_at = time (); $ user-> save ();...}, 'On afterlogin' => function ($ event) {// the same}],...]