自定義 Laravel 5.6 MethodNotAllowedHttpException /NotFoundHttpException 錯誤信息

use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

 

public function render($request, Exception $exception)
{
    // 接口請求方式不在路由設置中或不被容許時
    if ($exception instanceof MethodNotAllowedHttpException) {
        return \Response::json(['code' => 405, 'msg' => 'Method Not Allowed'], 405);
    }

    // 404異常擴展設置
    if ($exception instanceof NotFoundHttpException) {
        return \Response::json(['code' => 404, 'msg' => 'Not Found'], 404);
    }

    return parent::render($request, $exception);
}
相關文章
相關標籤/搜索