yii2.0用户登录,退出判断(摘录)
文章来源:http://blog.sina.com.cn/s/blog_88a65c1b0101ix13.html
判断用户是否登录
在 Yii2.0 里面,判断用户是否已经登录,我们用下面的代码即可
Yii::$app->user->isGuest;
示例:如果用户已经登录,直接调用 goHome() 方法
if (!\Yii::$app->user->isGuest) {
return $this->goHome();      
}
获取登录用户名
在 yii2.0 里面,获取登录状态下的用户名称,可以用下面的代码。
Yii::$app->user->identity->username;
用户退出操作我们用下面的方法
$this->user->logout();
使用示例:
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
下面是 logout() 方法的详细代码,可以做了解
public function logout($destroySession = true)    
{     
$identity = $this->getIdentity();     
if ($identity !== null && $this->beforeLogout($identity)) {     
$this->switchIdentity(null);     
$id = $identity->getId();     
$ip = Yii::$app->getRequest()->getUserIP();     
Yii::info("User '$id' logged out from $ip.", __METHOD__);     
if ($destroySession) {     
Yii::$app->getSession()->destroy();     
}     
$this->afterLogout($identity);     
}     
return $this->getIsGuest();     
}
                    
                
                
            
        
浙公网安备 33010602011771号