登录后,yii :: $ app-> user-> isGuest在main.php上始终为true

时间:2016-10-27 08:41:39

标签: yii2

我想在yii2 basic app中实现用户登录。当我尝试访问布局主页面上的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登录Mehod

 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.');
        }
    }

}

1 个答案:

答案 0 :(得分:2)

删除行:

private $id;
private $authKey;

来自User班。

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

  

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