Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Yii2认证_Php_Yii2 - Fatal编程技术网

Php Yii2认证

Php Yii2认证,php,yii2,Php,Yii2,我正在从基本安装修改User.php模型 这就是我现在拥有的: <?php namespace app\models; use yii\db\ActiveRecord; class User extends ActiveRecord implements \yii\web\IdentityInterface { public $id; public $username; public $password; /** * @inheritdo

我正在从基本安装修改User.php模型

这就是我现在拥有的:

<?php

namespace app\models;

use yii\db\ActiveRecord;

class User extends ActiveRecord implements \yii\web\IdentityInterface
{

    public $id;
    public $username;
    public $password;

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        return null;
    }

    /**
     * Finds user by username
     *
     * @param  string      $username
     * @return static|null
     */
    public static function findByUsername($username)
    {
        return static::findOne(['username' => $username]);
    }

    /**
     * @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 $this->password === \Yii::$app->getSecurity()->generatePasswordHash($password);
    }
}
我遇到的问题是,当程序到达User.php的validatePassword时,$this->password始终为null,验证失败

我确信找到了该记录,因为我可以看到LoginForm.php的$this->\u用户中的值。否则,当我试图找到某个随机用户时,它将为null


如何用静态函数中的参数填充User.php的$this->password?

您不必定义已经存储在User db表中的属性“id”、“username”、“password”。事实上,您正在用null重新定义数据库值

同时将validatePassword更改为


谢谢你说得对。我确实尝试删除声明,但删除后,$this->密码不存在。哦,等等。它确实存在。我会先做更多的测试。酷。这是问题之一。另一个是我使用了错误的验证方法。已更新您的答案以包含该内容。
public function getUser()
{
    if ($this->_user === false) {
        $this->_user = User::findByUsername($this->username);
    }

    return $this->_user;
}
/**
 * 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->getSecurity()->validatePassword($password, $this->password);
}