YII2.0 注册表格 Class 'Users' 不存在或语法错误

YII2.0 Register form Class 'Users' does not exist or has syntax error

我的模型文件夹中有 Users 模型,我必须通过 gii 创建一个新的注册表单,所以我在表单创建 gii 页面中输入了我的 class 名称。

但它抛出 class 名称不存在或语法错误。

Users.php代码为

namespace app\models;
use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;

/**
 * This is the model class for table "users".
 *
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property integer $user_type
 */

class Users extends \yii\db\ActiveRecord implements IdentityInterface
{
/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'users';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['username', 'password', 'user_type'], 'required'],
        [['user_type'], 'integer'],
        [['username', 'password','auth_key'], 'string', 'max' => 255]
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'username' => 'Username',
        'password' => 'Password',
        'user_type' => 'User Type',
    ];
}

/** INCLUDE USER LOGIN VALIDATION FUNCTIONS**/
/**
 * @inheritdoc
 */
public static function findIdentity($id)
{
    return static::findOne($id);
}

/**
 * @inheritdoc
 */
/* modified */
public static function findIdentityByAccessToken($token, $type = null)
{
    return static::findOne(['access_token' => $token]);
}

/* removed
    public static function findIdentityByAccessToken($token)
    {
        throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
    }
*/
/**
 * Finds user by username
 *
 * @param  string      $username
 * @return static|null
 */
public static function findByUsername($username)
{
    return static::findOne(['username' => $username]);
}

/**
 * Finds user by password reset token
 *
 * @param  string      $token password reset token
 * @return static|null
 */
public static function findByPasswordResetToken($token)
{
    $expire = \Yii::$app->params['user.passwordResetTokenExpire'];
    $parts = explode('_', $token);
    $timestamp = (int) end($parts);
    if ($timestamp + $expire < time()) {
        // token expired
        return null;
    }

    return static::findOne([
        'password_reset_token' => $token
    ]);
}

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

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

/**
 * @inheritdoc
 */
public function validateAuthKey($authKey)
{
    return $this->getAuthKey() === $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 === sha1($password);
    return $this->password === $password;
}

/**
 * Generates password hash from password and sets it to the model
 *
 * @param string $password
 */
public function setPassword($password)
{
    $this->password_hash = Security::generatePasswordHash($password);
}

/**
 * Generates "remember me" authentication key
 */
public function generateAuthKey()
{
    $this->auth_key = Security::generateRandomKey();
}

/**
 * Generates new password reset token
 */
public function generatePasswordResetToken()
{
    $this->password_reset_token = Security::generateRandomKey() . '_' . time();
}

/**
 * Removes password reset token
 */
public function removePasswordResetToken()
{
    $this->password_reset_token = null;
}
/** EXTENSION MOVIE **/
}

我可以知道那是什么问题吗,屏幕让你更好地理解。

对模型 class 使用完整的 class path/namespace 因为在另一个包中可能还有另一个 class 同名,所以你必须非常具体的。这都是关于包的,Model Class 上的工具提示也说了(您可以通过将鼠标悬停在标签模型 Class 上看到)。在这里查看关于 namespaces

您必须为模型 class 路径使用正确的语法。您可以通过将鼠标悬停在 "Model Class" 字段来找到正确的路径格式。您必须使用模型 Class 路径作为 "app\models\Users".

试试这个,让我知道你是否仍然面临同样的问题 query/concern。