Yii2 Yii::$app->;用户->;登录后main.php上的isGuest始终为true

Yii2 Yii::$app->;用户->;登录后main.php上的isGuest始终为true,yii2,Yii2,我想实现用户登录到yii2基本应用程序。除了我尝试访问版面主页上的Yii::$app->user->isGuest时,所有操作都正常。它总是返回真的。这里怎么了?请帮帮我 public function actionLogin() { if (!Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm

我想实现用户登录到yii2基本应用程序。除了我尝试访问版面主页上的
Yii::$app->user->isGuest
时,所有操作都正常。它总是返回真的。这里怎么了?请帮帮我

 public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();

        if ($model->load(Yii::$app->request->post()) && $model->login()) { 
            Yii::$app->user->isGuest;  // i m getting this as false, which is correct, but after goBack(), I m getting it as true
            return $this->goBack();
        }
        return $this->render('login', [
            'model' => $model,
        ]);
    }
从LoginForm.php登录方法

 public function login()
    {
        if ($this->validate()) { 
            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
        }
        return false;
    }
注意:我使用的是自定义主题,它位于
web
文件夹外和
project/themes/
目录内

**用户模型如下**

<?php

namespace app\models;

use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\web\NotFoundHttpException;

class User extends ActiveRecord implements IdentityInterface {

    private $id;
    private $authKey;

    const STATUS_DELETED = '0';
    const STATUS_ACTIVE = '10';

    public static function tableName() {
        return '{{%user}}';
    }

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return [
            TimestampBehavior::className(),
        ];
    }

    /**
     * @inheritdoc
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @inheritdoc
     */
    public function getAuthKey() {
        return $this->authKey;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey) {
        return $this->authKey === $authKey;
    }

    /**
     * Validates password
     *
     * @param string $password password to validate
     * @return boolean if password provided is valid for current user
     */

    public function validatePassword($password) {
        return Yii::$app->security->validatePassword($password, $this->password_hash);
    }


    public static function findByEmail($email) {
        $user_type = ['U'];
        return static::find()
                        ->andWhere('email = :email', [':email' => $email])
                        ->andFilterWhere(['in', 'user_type', $user_type])
                        ->one();
    }

    public static function findIdentity($id) {
        $user = static::find()->where(['id' => $id, 'status' => self::STATUS_ACTIVE,])->one();
        if (empty($user->id)) {
            \Yii::$app->session->destroy();
        }
        return $user;
    }

    public static function findIdentityByAccessToken($token, $type = null) {

        $user = static::find()
                ->where([
                    'access_token' => $token,
                    'status' => self::STATUS_ACTIVE,
                ])
                ->one();
        if (!empty($user)) {
            return $user;
        } else {
            throw new NotFoundHttpException('Invalid access token.');
        }
    }

}
删除以下行:

private $id;
private $authKey;
用户

您不应该直接声明来自数据库的ActiveRecord属性,如中所述

注意:活动记录属性以区分大小写的方式以关联的表列命名。Yii在活动记录中为关联表的每一列自动定义一个属性。您不应该重新声明任何属性


因此,您可以在布局文件中或在这个
actionLogin
中检查它的值。在登录内部,我得到它为false,但在重定向到layout/main.php之后,它的get-trueShow-us
login()
方法来自
LoginForm
。我没有修改它。请参考更新的代码首先删除
private$id;私人$authKey来自
用户
并再次检查。这
if(empty($user->id)){\Yii::$app->session->destroy();}
也不需要。