2016-07-04 40 views
1

我想只允許GET請求到我的控制器,並且我附加了VerbFilter。該文檔說,當請求的方法不被允許時,它必須返回405 http狀態碼,但是我得到了500個狀態碼。Yii2 VerbFilter返回500狀態代碼而不是405

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錯誤是當前錯誤的重複。我對它的一個原因沒有想法。

+0

500服務器錯誤,所以我想你有錯誤。請求使用GET工作是否成功? –

+0

@Jørgen是的,GET工作成功。錯誤消息在處理另一個錯誤時告訴錯誤。所以,VerbFilter會給出兩個錯誤。就像我附加了兩次VerbFilter。但我不知道。 Wtf ... :( –

+0

提供日誌爲500錯誤 –

回答

1

第一個錯誤:「Previous Error」 - 405是錯誤處理程序中的重定向。 第二個錯誤:你的錯誤動作要還「GET」請求,但看起來有相同類型的請求=>無限循環

您動詞篩選器中指定的動作,您將收到405錯誤

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

是的,它的工作原理我允許所有方法的錯誤操作和GET其他行動和它的作品。謝謝! –

相關問題