How does YII2 use afterLogin and beforLogin to automatically update the Last Logon Time?

Source: Internet
Author: User
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...

FoundYii2YesafterLoginAndbeforLoginTwo methods (events), but do not know how to use them.

InUser ModelI have written the following code:

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'                ],            ]        ];    }

BecauseActiveRecordOnly add, delete, modify, and query database events.yii\web\User::EVENT_AFTER_LOGINIt's useless to add it here...

Reply content:

FoundYii2YesafterLoginAndbeforLoginTwo methods (events), but do not know how to use them.

InUser ModelI have written the following code:

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'                ],            ]        ];    }

BecauseActiveRecordOnly add, delete, modify, and query database events.yii\web\User::EVENT_AFTER_LOGINIt'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}],...]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.