demo 2015-12-09 1700次浏览

在你的项目中的config下的main.php中增加如下代码:

return [
    ......
    'components' => [
        ............
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'on beforeLogin'=>function($event){
                $user=$event->identity;
                $user->last_login_time    =    time();
                .......
                $user->save();
            },
            'on beforeLogout'=>function($event){
                $user=$event->identity;
                $user->logout_time    =    time();
                .......
                $user->save();
            }
        ],
       ......
    ],
    .......
];

除了afterLogin,beforeLogout外,还有beforeLogin和afterLogout