2015-05-16 25 views

Trả lời

14
// Exceptions/Handler.php 

use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; 

public function render($request, \Exception $e) 
{ 
    if ($e instanceof MethodNotAllowedHttpException) { 
     // … 
    } 

    return parent::render($request, $e); 
} 
+0

Nếu bạn gặp sự cố khi sử dụng 'code'instanceof'code' với php 7.1, bạn có thể sử dụng' code'is_a() 'code'. Nếu ai đó có một lời giải thích tại sao tôi không nhận được 'code'true'code' với' code'instanceof'code'? Cảm ơn. –

3

Trong Laravel 5.4, tôi đã làm điều đó như thế này:

file location: app/Exceptions/Handler.php

Thêm mã này ở phía trên cùng của tập tin:

use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; 

Và thay đổi mã phương pháp như belows:

/** 
    * Render an exception into an HTTP response. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \Exception $exception 
    * @return \Illuminate\Http\Response 
    */ 
    public function render($request, Exception $exception) 
    { 
     if ($exception instanceof MethodNotAllowedHttpException) 
     { 
      return response()->json([ 
             'success' => 0, 
             'message' => 'Method is not allowed for the requested route', 
            ], 405); 
     } 

     return parent::render($request, $exception); 
    } 
Các vấn đề liên quan