Yii2 VerbFilter返回500状态代码而不是405

时间:2016-07-04 09:49:55

标签: yii2 yii2-basic-app

我想只允许GET请求到我的控制器,我附加了VerbFilter。文档说,当请求的方法不允许时,它必须返回405 http状态代码,但我获得了500个状态代码。

class MyController extends Controller {

   ...

   public function behaviors(){
     return [
       'verb' => [
         'class' => VerbFilter::className(),
         'actions' => [ '*' => ['get'] ]
     ];
   }

   public function actions(){
      return [ 'error' => [
        'class' => 'yii\web\ErrorAction'
      ]];
   }

   ...

   }

错误消息

An Error occurred while handling another error: 
exception 'yii\web\MethodNotAllowedHttpException' with message 
'Method Not Allowed. This url can only handle the following request methods: GET.' 
in /yii_project/vendor/yiisoft/yii2/filters/VerbFilter.php:105

Previous exception:
exception 'yii\web\MethodNotAllowedHttpException' with message 
'Method Not Allowed. This url can only handle the following request methods: GET.' 
in /yii_project/vendor/yiisoft/yii2/filters/VerbFilter.php:105

如您所见,previos错误是当前错误的重复。我对它的原因一无所知。

1 个答案:

答案 0 :(得分:1)

第一个错误:“Previous Error” - 是405,错误处理程序重定向。 第二个错误:您的错误操作也需要'GET'请求,但看起来有相同类型的请求。=>无限循环

为动词过滤器指定操作,您将收到405错误

public function behaviors(){
     return [
       'verb' => [
         'class' => VerbFilter::className(),
         'actions' => [ 'action-name' => ['get'] ]
     ];
   }