Иван Апевалов Asked:2024-05-21 19:29:22 +0800 CST2024-05-21 19:29:22 +0800 CST 2024-05-21 19:29:22 +0800 CST 设置 APP_DEBUG=false 时如何显示异常消息? 772 当设置 APP_DEBUG=false 时,如何让 Laravel 中出现异常消息?现在它只返回服务器错误 - 500 laravel 1 个回答 Voted Best Answer Condor 2024-05-23T01:07:54+08:002024-05-23T01:07:54+08:00 为此,您需要设置自己的异常处理程序并输出必要的数据。在其处理程序中,或在默认处理程序 (App\Exceptions\Handler) 中。自定义处理程序的示例: <?php namespace App\Exceptions; use Exception; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Symfony\Component\HttpKernel\Exception\HttpException; class CustomExceptionHandler extends ExceptionHandler { public function render($request, Exception $exception) { if ($exception instanceof HttpException && !$this->isHttpException($exception)) { if (!config('app.debug')) { return response()->view('errors.custom', [ 'message' => $exception->getMessage(), ], $exception->getStatusCode()); } } return parent::render($request, $exception); } } 其中可以通过blade模板连接错误输出,或者干脆以json的形式发送: return response()->json([ 'message' => $exception->getMessage(), ], $exception->getStatusCode());
为此,您需要设置自己的异常处理程序并输出必要的数据。在其处理程序中,或在默认处理程序 (App\Exceptions\Handler) 中。自定义处理程序的示例:
其中可以通过blade模板连接错误输出,或者干脆以json的形式发送: