yii2 禁用内置身份验证

yii2 disable inbuilt authentication

我已经编写了自己的 Users 模型,它扩展了 ActiveRecord 并实现了 IdentityInterface 并且还定义了表名。并实施了所有方法。

Users.php

public static function tableName()
    {
        return 'users';
    }
// other methods are also present like rules() , getId() etc.

UserController.php

public function actionLogin()
    {
        $this->layout = 'blank';

        if (!Yii::$app->myuser->isGuest) {
            return 'hello';
        }


        $model = new UserLoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->redirect(['user/view',
                'id' => $model->getUser()->id,
            ]);
        } else {
            $model->password = '';

            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

UserLoginForm.php

<?php

namespace backend\models;

use Yii;
use yii\base\Model;

class UserLoginForm extends Model
{

    public $username;
    public $password;
    public $rememberMe = true;

    private $_user;


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
            // rememberMe must be a boolean value
            ['rememberMe', 'boolean'],
            // password is validated by validatePassword()
            ['password', 'validatePassword'],
        ];
    }

    /**
     * Validates the password.
     * This method serves as the inline validation for password.
     *
     * @param string $attribute the attribute currently being validated
     * @param array $params the additional name-value pairs given in the rule
     */
    public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();
            if (!$user || !$user->validatePassword($this->password)) {
                $this->addError($attribute, 'Incorrect username or password.');
            }
        }
    }

    /**
     * Logs in a user using the provided username and password.
     *
     * @return bool whether the user is logged in successfully
     */
    public function login()
    {
        if ($this->validate()) {
            return Yii::$app->myuser->login($this->getUser());
        }

        return false;
    }

    /**
     * Finds user by [[username]]
     *
     * @return Users|null
     */
    public function getUser()
    {
        if ($this->_user === null) {
            $this->_user = Users::findByUsername($this->username);
        }

        return $this->_user;
    }
}

并在 backend/config/main.php

'myuser' => [
            'class' => 'yii\web\User',
            'identityClass' => 'backend\models\Users',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-backend_user', 'httpOnly' => true],
        ],

但是登录成功后,出现如下错误

The table does not exist: {{%user}}

我发现它正在调用 common/models/User.php class,默认情况下出现在高级模板中。但为什么它调用这个 class ?我想使用我自己的 Users.php 模型。请有人帮我解决这个问题。

用于认证的class由Yii2指南的user application component, according to the authentication section决定:

The user application component manages the user authentication status. It requires you to specify an identity class which contains the actual authentication logic. In the following application configuration, the identity class for user is configured to be app\models\User whose implementation is explained in the next subsection:

当你配置一个新的myuser组件时,框架仍然使用默认配置的user组件,更新你的代码,所以它会覆盖user组件,而不是创建一个新的 myuser

'user' => [
    // Points to your custom class
    'identityClass' => 'backend\models\Users',
    'enableAutoLogin' => true,
    'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
],