如何在yii2restapi中进行基本身份验证

如何在yii2restapi中进行基本身份验证,rest,yii,Rest,Yii,我已经试过了,但对我没有帮助: 以下是我所做的: 我有BaseController和DocumentController扩展它 在BaseController中,我有以下代码: public function behaviors() { $behaviors = parent::behaviors(); $behaviors['contentNegotiator'] = [ 'class' => 'yii\filters\ContentNegotiator

我已经试过了,但对我没有帮助:

以下是我所做的:

我有BaseController和DocumentController扩展它

在BaseController中,我有以下代码:

public function behaviors()
{
    $behaviors = parent::behaviors();

    $behaviors['contentNegotiator'] = [
        'class' => 'yii\filters\ContentNegotiator',
        'formats' => [
            'application/json' => Response::FORMAT_JSON,
        ]
    ];

    $behaviors['authenticator'] = [
        'class' => HttpBasicAuth::className(),
        'auth' => [$this, 'authenticate']
    ];

    return $behaviors;
}

/**
 * Finds user by username and password
 *
 * @param string $username
 * @param string $password
 * @return static|null
 */
public function authenticate($username, $password) 
{
    // username, password are mandatory fields
    if(empty($username) || empty($password)) {
        return null;
    }

    // get user using requested email
    $user = User::findByUsername($username);

    // if no record matching the requested user
    if(empty($user)) {
        return null;
    }

    // if password validation fails
    if(!User::validatePassword($password)) {
        return null;
    }

    // if user validates (both user_email, user_password are valid)
    return $user;
} 
当我尝试访问我的api时:
localhost/api/documents/5

我收到以下错误:
获取未知属性:app\\controllers\\DocumentController::password\u hash

我该怎么做才能让这件事成功?我只是不明白yii想从我这里得到什么

编辑:完全错误:

{"name":"Unknown Property","message":"Getting unknown property: app\\controllers\\DocumentController::password_hash","code":0,"type":"yii\\base\\UnknownPropertyException","file":"/var/www/html/api/_protected/vendor/yiisoft/yii2/base/Component.php","line":143,"stack-trace":["#0 /var/www/html/api/_protected/models/UserIdentity.php(129): yii\\base\\Component->__get('password_hash')","#1 /var/www/html/api/_protected/controllers/AppController.php(58): app\\models\\UserIdentity->validatePassword('wspass')","#2 [internal function]: app\\controllers\\AppController->authenticate('wsuser', 'wspass')","#3 /var/www/html/api/_protected/vendor/yiisoft/yii2/filters/auth/HttpBasicAuth.php(68): call_user_func(Array, 'wsuser', 'wspass')","#4 /var/www/html/api/_protected/vendor/yiisoft/yii2/filters/auth/AuthMethod.php(60): yii\\filters\\auth\\HttpBasicAuth->authenticate(Object(yii\\web\\User), Object(yii\\web\\Request), Object(yii\\web\\Response))","#5 /var/www/html/api/_protected/vendor/yiisoft/yii2/base/ActionFilter.php(71): yii\\filters\\auth\\AuthMethod->beforeAction(Object(yii\\rest\\IndexAction))","#6 [internal function]: yii\\base\\ActionFilter->beforeFilter(Object(yii\\base\\ActionEvent))","#7 /var/www/html/api/_protected/vendor/yiisoft/yii2/base/Component.php(541): call_user_func(Array, Object(yii\\base\\ActionEvent))","#8 /var/www/html/api/_protected/vendor/yiisoft/yii2/base/Controller.php(269): yii\\base\\Component->trigger('beforeAction', Object(yii\\base\\ActionEvent))","#9 /var/www/html/api/_protected/vendor/yiisoft/yii2/web/Controller.php(108): yii\\base\\Controller->beforeAction(Object(yii\\rest\\IndexAction))","#10 /var/www/html/api/_protected/vendor/yiisoft/yii2/base/Controller.php(152): yii\\web\\Controller->beforeAction(Object(yii\\rest\\IndexAction))","#11 /var/www/html/api/_protected/vendor/yiisoft/yii2/base/Module.php(454): yii\\base\\Controller->runAction('index', Array)","#12 /var/www/html/api/_protected/vendor/yiisoft/yii2/web/Application.php(84): yii\\base\\Module->runAction('document/index', Array)","#13 /var/www/html/api/_protected/vendor/yiisoft/yii2/base/Application.php(375): yii\\web\\Application->handleRequest(Object(yii\\web\\Request))","#14 /var/www/html/api/index.php(12): yii\\base\\Application->run()","#15 {main}"]}

请填写完整的错误消息。您应该在相关文件中搜索
密码\u散列
@mohit;我更新了一篇文章,完全是错误的。但我不明白yii为什么抱怨DocumentController中的密码\u散列。根据错误消息,您正在将密码\u散列作为DocumentController属性访问,但此控制器中未定义密码\u散列。检查您是否在控制器中编写了
$this->password\u hash
种类代码,因为$this表示控制器类。它应该由$model class对象访问。您可能也希望签入此文件-
var/www/html/api/\u protected/models/UserIdentity.php(129)