我正在使用Yii2框架构建API,我需要告诉yii一些行为充当公共行为。 我添加了除了我的控制器的行为功能,但它不起作用
public function behaviors() {
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBearerAuth::className(),
'except' => ['NotifyOrder'],
];
return $behaviors;
}
public function actionNotifyOrder() {
echo 1;
}
当我拨打/ notify-order url
时,我总是收到以下错误<response><name>Unauthorized</name><message>Your request was made with invalid credentials.</message><code>0</code><status>401</status><type>yii\web\UnauthorizedHttpException</type></response>
答案 0 :(得分:0)
根据docs您需要告诉它操作ID (网址中使用的以虚线分隔的格式)
你应该$behaviors['authenticator'] = [
'class' => HttpBearerAuth::className(),
'except' => ['notify-order', 'another-action', 'and-so-on'],
];