我在动词行为中添加一个动词,只允许对某些动作发出POST请求,如果请求不是POST,则应该返回不允许的方法但是这不起作用它返回404找不到响应而不是405 Not当我发送GET请求而不是POST请求任何帮助
时允许响应我在我的REST控件中设置了这样的行为
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => CompositeAuth::className(),
'except' => ['login'],
'authMethods' => [
HttpBearerAuth::className(),
],
];
$behaviors['verbs'] = [
'class' => \yii\filters\VerbFilter::className(),
'actions' => [
//'index' => ['get'],
'login' => ['post', 'put'],
'view' => ['get'],
//'create' => ['get', 'post'],
'update' => ['put'],
//'delete' => ['post', 'delete'],
'delete' => [''],
'test', ['post']
],
];
return $behaviors;
}
在main.php中
'urlManager' => [
'enablePrettyUrl' => true,
'enableStrictParsing' => true,
'showScriptName' => false,
'rules' => [
[
'class' => 'yii\rest\UrlRule',
'controller' => ['v1/vendor'],
'extraPatterns' => [
'POST,PUT login' => 'login',
'POST logout' => 'logout',
'POST test' => 'test'
],
'tokens' => [
'{id}' => '<id:\\w+>'
]
],
],
]
所以当我测试并发送GET请求
时GET localhost/mywebsite/api/web/v1/vendors/test
它返回404而不是405状态请任何帮助
答案 0 :(得分:0)
'extraPatterns' => [
...
'test' => 'test'
]
答案 1 :(得分:-1)
从Yii2准则routing:漂亮的网址,严格的解析总是抛出\ yii \ web \ NotFound \ HttpException。 您可以在动作之前更新控制器:
ServiceLocatorAwareInterface