简体   繁体   English

成功登录后,用户未登录到YII2前端

[英]After successful login user is not logged into YII2 frontend

If I run the login function it prints User . 如果我运行登录功能,它将输出User

public function actionLogin(){
if ($model->load(Yii::$app->request->post()) && $model->login()) {
    if (Yii::$app->user->isGuest) {
        echo "guest";
    } else {
        echo "User";
    }
    return $this->redirect(['dashboard']);
}

After redirect If I run the dashboard function it prints guest . 重定向后如果运行仪表板功能,它将打印guest

public function actionDashboard()
{        
    if (Yii::$app->user->isGuest) {
        echo "guest";
    } else {
        echo "User";
    }
}

My Login model: 我的Login模型:

public function login()
{
    if ($this->validate()) {
        return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
    } else {
        return false;
    }
}        

This is getUser function: 这是getUser函数:

protected function getUser()    
{
    if ($this->_user === null) {
        $this->_user = Customer::findByEmail($this->email);
    }

    return $this->_user;
}

Please help me to check in another function whether or not the user is logged in? 请帮助我检查其他功能是否用户已登录?

I successfully fixed this problem. 我已成功解决此问题。

just change the backend/config/main.php or frontend/config/main.php 只需更改backend / config / main.php或frontend / config / main.php

change this 改变这个

'components' => [
        'user' => [
            'identityClass' => 'common\models\Customer', //or Any models you want
            'enableAutoLogin' => true,
        ],
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM