yii2 内置事件
1、yii2系统登录
const EVENT_BEFORE_LOGIN = 'beforeLogin'; //登录前
const EVENT_AFTER_LOGIN = 'afterLogin'; //登录后
const EVENT_BEFORE_LOGOUT = 'beforeLogout'; //退出前
const EVENT_AFTER_LOGOUT = 'afterLogout'; //退出后
2、事件绑定
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true,
'on afterLogin'=>function($event){
$user=$event->identity; $model=new Log_Login();
$model->USER_ID=$user->USER_ID;
$model->USER_AGENT=$_SERVER['HTTP_USER_AGENT'];
$model->IP=$_SERVER['REMOTE_ADDR'];
$model->LOGIN=date("Y-m-d H:i:s");
$model->save();
},
'on beforeLogout'=>function ($event){
//$user=$event->identity;
$model = Log_Login::findOne((int)$_SESSION['log_id']);
$model->LOGOUT=date("Y-m-d H:i:s");
$model->update();
} ]
注意:如果获取的系统时间与当前时间不符,在配置文件中设置时区'timeZone'=>'Asia/shanghai'