{"id":22867,"date":"2024-11-21T15:43:29","date_gmt":"2024-11-21T07:43:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22867\/"},"modified":"2024-11-21T15:43:29","modified_gmt":"2024-11-21T07:43:29","slug":"laravel-%e8%af%b7%e6%b1%82%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22867\/","title":{"rendered":"laravel \u8bf7\u6c42\u5f02\u5e38\u5904\u7406"},"content":{"rendered":"<p>laravel \u662f\u4e00\u79cd\u6d41\u884c\u7684 php \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u7cfb\u7edf\u6765\u6784\u5efa web \u5e94\u7528\u7a0b\u5e8f\u3002\u4f46\u662f\uff0c\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u96be\u514d\u4f1a\u9047\u5230\u8bf7\u6c42\u5f02\u5e38\u7684\u60c5\u51b5\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba laravel \u8bf7\u6c42\u5f02\u5e38\u7684\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n<ol>\n<li>\u5f02\u5e38\u7684\u5206\u7c7b<\/li>\n<\/ol>\n<p>Laravel \u4e2d\u8bf7\u6c42\u5f02\u5e38\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a\u7a0b\u5e8f\u5f02\u5e38\u548c HTTP \u5f02\u5e38\u3002<\/p>\n<p>\u7a0b\u5e8f\u5f02\u5e38\u662f\u5728\u4ee3\u7801\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u4f8b\u5982 PHP \u629b\u51fa\u7684\u81f4\u547d\u9519\u8bef\uff0c\u672a\u6355\u83b7\u7684\u5f02\u5e38\u7b49\u7b49\u3002<\/p>\n<p>HTTP \u5f02\u5e38\u662f\u6307\u5728 HTTP \u8bf7\u6c42\u4e2d\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u4f8b\u5982 404 Not Found\uff0c500 Internal Server Error \u7b49\u7b49\u3002<\/p>\n<p>\u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u9700\u8981\u91c7\u7528\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f\u3002<\/p>\n<ol>\n<li>\u7a0b\u5e8f\u5f02\u5e38\u7684\u5904\u7406<\/li>\n<\/ol>\n<p>\u7a0b\u5e8f\u5f02\u5e38\u53ef\u80fd\u4f1a\u51fa\u73b0\u5728 Laravel \u63a7\u5236\u5668\u4e2d\uff0c\u5982\u679c\u4e0d\u52a0\u5904\u7406\uff0c\u5c06\u4f1a\u5f39\u51fa\u4e00\u4e2a\u9875\u9762\u663e\u793a\u9519\u8bef\u6d88\u606f\u3002\u8fd9\u5e76\u4e0d\u662f\u7528\u6237\u671f\u671b\u770b\u5230\u7684\uff0c\u56e0\u6b64\u9700\u8981\u5bf9\u7a0b\u5e8f\u5f02\u5e38\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>Laravel \u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u4e24\u79cd\u65b9\u6cd5\u5904\u7406\u7a0b\u5e8f\u5f02\u5e38\u3002\u7b2c\u4e00\u79cd\u662f\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u5668\uff0c\u7b2c\u4e8c\u79cd\u662f\u4f7f\u7528\u5168\u5c40\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<p>2.1 \u5f02\u5e38\u5904\u7406\u5668<\/p>\n<p>Laravel \u5f02\u5e38\u5904\u7406\u5668\u662f\u4e00\u4e2a\u7c7b\uff0c\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u629b\u51fa\u7684\u5f02\u5e38\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u629b\u51fa\u5f02\u5e38\u65f6\u63a7\u5236\u5668\u8fd4\u56de\u4e00\u4e2a JSON \u683c\u5f0f\u7684\u54cd\u5e94\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u5668\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>&lt;?php namespace AppExceptions;\n\nuse Exception;\nuse IlluminateFoundationExceptionsHandler as ExceptionHandler;\n\nclass Handler extends ExceptionHandler\n{\n    \/**\n     * A list of the exception types that are not reported.\n     *\n     * @var array\n     *\/\n    protected $dontReport = [\n        \/\/\n    ];\n\n    \/**\n     * Report or log an exception.\n     *\n     * @param  Exception  $exception\n     * @return void\n     *\/\n    public function report(Exception $exception)\n    {\n        parent::report($exception);\n    }\n\n    \/**\n     * Render an exception into an HTTP response.\n     *\n     * @param  IlluminateHttpRequest  $request\n     * @param  Exception  $exception\n     * @return IlluminateHttpResponse\n     *\/\n    public function render($request, Exception $exception)\n    {\n        if ($exception instanceof IlluminateDatabaseEloquentModelNotFoundException) {\n            return response()-&gt;json([\n                'error' =&amp;gt; 'Resource not found'\n            ], 404);\n        }\n\n        return parent::render($request, $exception);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u7ee7\u627f\u4e86 Laravel \u7684\u5f02\u5e38\u5904\u7406\u5668\u7c7b\uff0c\u5e76\u91cd\u5199\u4e86 render \u65b9\u6cd5\u3002\u5728 render \u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u4e86\u5f02\u5e38\u7c7b\u578b\u662f\u5426\u662f IlluminateDatabaseEloquentModelNotFoundException\u3002\u5982\u679c\u662f\uff0c\u6211\u4eec\u8fd4\u56de\u4e00\u4e2a JSON \u683c\u5f0f\u7684\u54cd\u5e94\u3002<\/p>\n<p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u5904\u7406\u5176\u4ed6\u7684\u7a0b\u5e8f\u5f02\u5e38\u3002\u8fd9\u79cd\u5904\u7406\u65b9\u5f0f\u7684\u597d\u5904\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u4e3a\u6bcf\u79cd\u7c7b\u578b\u7684\u5f02\u5e38\u7f16\u5199\u81ea\u5b9a\u4e49\u7684\u5904\u7406\u5668\u3002\u8fd9\u6837\u6211\u4eec\u5c31\u80fd\u591f\u9884\u6d4b\u5230\u6211\u4eec\u4f1a\u5f97\u5230\u4ec0\u4e48\u6837\u7684\u54cd\u5e94\u3002<\/p>\n<p>2.2 \u5168\u5c40\u5f02\u5e38\u5904\u7406<\/p>\n<p>\u4f7f\u7528\u5168\u5c40\u5f02\u5e38\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u6355\u83b7\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u6240\u6709\u5f02\u5e38\uff0c\u800c\u4e0d\u662f\u4e3a\u6bcf\u79cd\u5f02\u5e38\u7f16\u5199\u5355\u72ec\u7684\u5904\u7406\u5668\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>&lt;?php namespace AppExceptions;\n\nuse Exception;\nuse IlluminateFoundationExceptionsHandler as ExceptionHandler;\n\nclass Handler extends ExceptionHandler\n{\n    \/**\n     * A list of the exception types that are not reported.\n     *\n     * @var array\n     *\/\n    protected $dontReport = [\n        \/\/\n    ];\n\n    \/**\n     * Report or log an exception.\n     *\n     * @param  Exception  $exception\n     * @return void\n     *\/\n    public function report(Exception $exception)\n    {\n        parent::report($exception);\n    }\n\n    \/**\n     * Render an exception into an HTTP response.\n     *\n     * @param  IlluminateHttpRequest  $request\n     * @param  Exception  $exception\n     * @return IlluminateHttpResponse\n     *\/\n    public function render($request, Exception $exception)\n    {\n        if ($exception instanceof SymfonyComponentHttpKernelExceptionHttpException) {\n            $code = $exception-&gt;getStatusCode();\n\n            return response()-&amp;gt;json([\n                'error' =&amp;gt; 'HTTP Exception',\n                'status' =&amp;gt; $code\n            ], $code);\n        }\n\n        return parent::render($request, $exception);\n    }\n\n    \/**\n     * Render the given HttpException.\n     *\n     * @param  SymfonyComponentHttpKernelExceptionHttpException  $e\n     * @return IlluminateHttpResponse\n     *\/\n    protected function renderHttpException(HttpException $e)\n    {\n        $status = $e-&amp;gt;getStatusCode();\n\n        if (view()-&amp;gt;exists(\"errors.{$status}\")) {\n            return response()-&amp;gt;view(\"errors.{$status}\", ['exception' =&amp;gt; $e], $status, $e-&amp;gt;getHeaders());\n        } else {\n            return $this-&amp;gt;convertExceptionToResponse($e);\n        }\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u91cd\u5199\u4e86 render \u65b9\u6cd5\uff0c\u68c0\u67e5\u5f02\u5e38\u7c7b\u578b\u662f\u5426\u662f SymfonyComponentHttpKernelExceptionHttpException\u3002\u5982\u679c\u662f\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a JSON \u683c\u5f0f\u7684\u54cd\u5e94\uff0c\u5305\u62ec\u9519\u8bef\u6d88\u606f\u548c HTTP \u72b6\u6001\u7801\u3002<\/p>\n<p>\u5982\u679c\u6211\u4eec\u9700\u8981\u5448\u73b0 HTML \u9875\u9762\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u91cd\u5199 renderHttpException \u65b9\u6cd5\uff0c\u4ee5\u6e32\u67d3\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u9875\u9762\u3002<\/p>\n<ol>\n<li>HTTP \u5f02\u5e38\u7684\u5904\u7406<\/li>\n<\/ol>\n<p>Laravel \u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u5904\u7406 HTTP \u5f02\u5e38\u3002\u901a\u8fc7\u81ea\u5b9a\u4e49 app\/Exceptions\/Handler.php \u4e2d\u7684 render \u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u8fd4\u56de\u6307\u5b9a\u7684 HTTP \u72b6\u6001\u7801\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>public function render($request, Exception $exception)\n{\n    if ($this-&amp;gt;isHttpException($exception)) {\n        return $this-&amp;gt;renderHttpException($exception);\n    } else {\n        return parent::render($request, $exception);\n    }\n}\n\nprotected function renderHttpException(HttpException $exception)\n{\n    return response()-&amp;gt;view('errors.' . $exception-&amp;gt;getStatusCode(), [], $exception-&amp;gt;getStatusCode());\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u68c0\u67e5\u5f02\u5e38\u662f\u5426\u662f HTTP \u5f02\u5e38\u3002\u5982\u679c\u662f\uff0c\u6211\u4eec\u4f7f\u7528 getStatusCode \u65b9\u6cd5\u83b7\u53d6 HTTP \u72b6\u6001\u7801\uff0c\u5e76\u5c06\u5176\u7528\u4f5c\u89c6\u56fe\u540d\u79f0\u3002\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u53ea\u662f\u8fd4\u56de\u4e86\u4e00\u4e2a\u5bf9\u5e94\u72b6\u6001\u7801\u7684\u89c6\u56fe\u3002<\/p>\n<ol>\n<li>\u7ed3\u8bba<\/li>\n<\/ol>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86 Laravel \u4e2d\u7a0b\u5e8f\u548c HTTP \u5f02\u5e38\u7684\u5904\u7406\u65b9\u6cd5\u3002\u6211\u4eec\u5b66\u4e60\u4e86\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u5668\u548c\u5168\u5c40\u5f02\u5e38\u5904\u7406\u6765\u5904\u7406\u7a0b\u5e8f\u5f02\u5e38\uff0c\u4ee5\u53ca\u5982\u4f55\u81ea\u5b9a\u4e49 render \u65b9\u6cd5\u6765\u5904\u7406 HTTP \u5f02\u5e38\u3002\u5bf9\u4e8e Laravel \u5f00\u53d1\u4eba\u5458\u6765\u8bf4\uff0c\u6b63\u786e\u5904\u7406\u5f02\u5e38\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u6280\u672f\uff0c\u6211\u4eec\u80fd\u591f\u66f4\u52a0\u7cbe\u786e\u5730\u63a7\u5236\u5e94\u7528\u7a0b\u5e8f\u7684\u884c\u4e3a\uff0c\u63d0\u9ad8\u5176\u53ef\u9760\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel \u8bf7\u6c42\u5f02\u5e38\u5904\u7406\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>laravel \u662f\u4e00\u79cd\u6d41\u884c\u7684 php \u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u7cfb\u7edf\u6765\u6784\u5efa web \u5e94\u7528\u7a0b\u5e8f\u3002\u4f46\u662f\uff0c\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u96be\u514d\u4f1a\u9047\u5230\u8bf7\u6c42\u5f02\u5e38\u7684\u60c5\u51b5\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8ba8\u8bba laravel \u8bf7\u6c42\u5f02\u5e38\u7684\u5904\u7406\u65b9\u6cd5\u3002 \u5f02\u5e38\u7684\u5206\u7c7b Laravel \u4e2d\u8bf7\u6c42\u5f02\u5e38\u53ef\u4ee5\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b\uff1a\u7a0b\u5e8f\u5f02\u5e38\u548c HTTP \u5f02\u5e38\u3002 \u7a0b\u5e8f\u5f02\u5e38\u662f\u5728\u4ee3\u7801\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u4f8b\u5982 PHP \u629b\u51fa\u7684\u81f4\u547d\u9519\u8bef\uff0c\u672a\u6355\u83b7\u7684\u5f02\u5e38\u7b49\u7b49\u3002 HTTP \u5f02\u5e38\u662f\u6307\u5728 HTTP \u8bf7\u6c42\u4e2d\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u4f8b\u5982 404 Not Found\uff0c500 Internal Server Error \u7b49\u7b49\u3002 \u4e0d\u540c\u7c7b\u578b\u7684\u5f02\u5e38\u9700\u8981\u91c7\u7528\u4e0d\u540c\u7684\u5904\u7406\u65b9\u5f0f\u3002 \u7a0b\u5e8f\u5f02\u5e38\u7684\u5904\u7406 \u7a0b\u5e8f\u5f02\u5e38\u53ef\u80fd\u4f1a\u51fa\u73b0\u5728 Laravel \u63a7\u5236\u5668\u4e2d\uff0c\u5982\u679c\u4e0d\u52a0\u5904\u7406\uff0c\u5c06\u4f1a\u5f39\u51fa\u4e00\u4e2a\u9875\u9762\u663e\u793a\u9519\u8bef\u6d88\u606f\u3002\u8fd9\u5e76\u4e0d\u662f\u7528\u6237\u671f\u671b\u770b\u5230\u7684\uff0c\u56e0\u6b64\u9700\u8981\u5bf9\u7a0b\u5e8f\u5f02\u5e38\u8fdb\u884c\u5904\u7406\u3002 Laravel \u7ed9\u6211\u4eec\u63d0\u4f9b\u4e86\u4e24\u79cd\u65b9\u6cd5\u5904\u7406\u7a0b\u5e8f\u5f02\u5e38\u3002\u7b2c\u4e00\u79cd\u662f\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u5668\uff0c\u7b2c\u4e8c\u79cd\u662f\u4f7f\u7528\u5168\u5c40\u5f02\u5e38\u5904\u7406\u3002 2.1 \u5f02\u5e38\u5904\u7406\u5668 Laravel \u5f02\u5e38\u5904\u7406\u5668\u662f\u4e00\u4e2a\u7c7b\uff0c\u5904\u7406\u5e94\u7528\u7a0b\u5e8f\u629b\u51fa\u7684\u5f02\u5e38\u3002\u5982\u679c\u6211\u4eec\u60f3\u8981\u629b\u51fa\u5f02\u5e38\u65f6\u63a7\u5236\u5668\u8fd4\u56de\u4e00\u4e2a JSON \u683c\u5f0f\u7684\u54cd\u5e94\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u5668\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a &lt;?php namespace AppExceptions; use Exception; use IlluminateFoundationExceptionsHandler as ExceptionHandler; class Handler extends ExceptionHandler { \/** [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-22867","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=22867"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22867\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}