2017-02-02 7 views
0

laravelが新しく、誤ったHTTPメソッドを使用してユーザーがURLにヒットした場合に例外を処理したいと考えています。私はjsonでユーザーの応答を送信したいが、コードは機能していない、時には空白ページを与える。以下は私のコードです:Laravel 5.3を使用してRESTful APIで 'MethodNotAllowedHttpException'を管理します。

Handler.php事前に

public function render($request, Exception $exception) 
    { 

     if ($exception instanceof MethodNotAllowedHttpException) { 
      return response()->json(['error' => 'Bad Request.'], 404); 
     } 

    } 

おかげ

答えて

0

これを試してみてください。

public function render($request, Exception $exception) 
    { 

     if (basename(str_replace('\\', '/', get_class($exception))) == 'MethodNotAllowedHttpException') { 
      return response()->json(['error' => 'Bad Request.'], 404);   
     } 
     return parent::render($request, $exception); 
    } 
関連する問題