{"id":22835,"date":"2024-11-21T08:31:15","date_gmt":"2024-11-21T00:31:15","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22835\/"},"modified":"2024-11-21T08:31:15","modified_gmt":"2024-11-21T00:31:15","slug":"laravel-%e9%94%99%e8%af%af%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22835\/","title":{"rendered":"laravel \u9519\u8bef\u5904\u7406"},"content":{"rendered":"<p>laravel\u662f\u4e00\u4e2a\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f88\u591a\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u8f7b\u677e\u5730\u8c03\u8bd5\u548c\u5904\u7406\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8laravel\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002<\/p>\n<ol>\n<li>\u914d\u7f6e\u6587\u4ef6<\/li>\n<\/ol>\n<p>Laravel\u7684\u914d\u7f6e\u6587\u4ef6config\/app.php\u4e2d\u6709\u4e00\u4e2akey\u503c\u4e3adebug\u7684\u9009\u9879\u3002\u5f53debug\u4e3atrue\u65f6\uff0cLaravel\u4f1a\u663e\u793a\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u4fbf\u4e8e\u5f00\u53d1\u4eba\u5458\u8c03\u8bd5\u7a0b\u5e8f\u3002\u5f53\u8bbe\u7f6e\u4e3afalse\u65f6\uff0c\u5219\u53ea\u663e\u793a\u4e00\u822c\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<ol>\n<li>\u5f02\u5e38\u5904\u7406<\/li>\n<\/ol>\n<p>Laravel\u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u6355\u83b7\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u3002\u5f53\u7a0b\u5e8f\u4e2d\u53d1\u751f\u9519\u8bef\u65f6\uff0cLaravel\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff0c\u8fd9\u4e2a\u5f02\u5e38\u5c06\u88ab\u7cfb\u7edf\u5904\u7406\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5728app\/Exceptions\/Handler.php\u6587\u4ef6\u4e2d\u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u5f02\u5e38\u3002\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6709\u4e00\u4e2a\u65b9\u6cd5\u53eb\u505arender\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528\u4ee5\u6e32\u67d3\u5f02\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u5982\u4f55\u5904\u7406\u5f02\u5e38\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre>public function render($request, Exception $exception)\n{\n    if ($exception instanceof IlluminateDatabaseEloquentModelNotFoundException) {\n        return response()-&amp;gt;json([\n            'error' =&amp;gt; 'Resource not found'\n        ], 404);\n    }\n\n    return parent::render($request, $exception);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5224\u65ad\u4e86\u5f02\u5e38\u7c7b\u578b\uff0c\u5982\u679c\u662fModelNotFoundException\uff0c\u5c31\u8fd4\u56de\u4e00\u4e2a404\u9519\u8bef\u548c\u4e00\u4e2ajson\u683c\u5f0f\u7684\u54cd\u5e94\uff0c\u544a\u8bc9\u5ba2\u6237\u7aef\u8d44\u6e90\u672a\u627e\u5230\u3002\u5982\u679c\u4e0d\u662f\u8fd9\u4e2a\u5f02\u5e38\u7c7b\u578b\uff0c\u5c31\u8c03\u7528\u7236\u7c7b\u7684render\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<ol>\n<li>\u65e5\u5fd7\u8bb0\u5f55<\/li>\n<\/ol>\n<p>\u9664\u4e86\u5728\u63a7\u5236\u53f0\u6216\u8005\u6d4f\u89c8\u5668\u4e0a\u663e\u793a\u9519\u8bef\u4fe1\u606f\uff0cLaravel\u8fd8\u652f\u6301\u5c06\u9519\u8bef\u4fe1\u606f\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8ffd\u8e2a\u548c\u8c03\u8bd5\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n<p>Laravel\u9ed8\u8ba4\u4f7f\u7528Monolog\u5e93\u6765\u8bb0\u5f55\u65e5\u5fd7\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u65e5\u5fd7\u9009\u9879\u6765\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7ea7\u522b\u548c\u65e5\u5fd7\u6587\u4ef6\u4f4d\u7f6e\u3002<\/p>\n<pre>'log' =&amp;gt; env('APP_LOG', 'single'),\n\n'log_level' =&amp;gt; env('APP_LOG_LEVEL', 'debug'),\n\n'channels' =&amp;gt; [\n        'stack' =&amp;gt; [\n            'driver' =&amp;gt; 'stack',\n            'channels' =&amp;gt; ['single'],\n        ],\n\n        'single' =&amp;gt; [\n            'driver' =&amp;gt; 'single',\n            'path' =&amp;gt; storage_path('logs\/laravel.log'),\n            'level' =&amp;gt; 'debug',\n        ],\n]<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Laravel\u4f1a\u5c06\u65e5\u5fd7\u8bb0\u5f55\u5230\/storage\/logs\/laravel.log\u6587\u4ef6\u4e2d\uff0c\u5e76\u4e14\u8bb0\u5f55\u6240\u6709debug\u7ea7\u522b\u53ca\u4ee5\u4e0a\u7684\u65e5\u5fd7\u3002<\/p>\n<ol>\n<li>\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762<\/li>\n<\/ol>\n<p>\u5f53\u7a0b\u5e8f\u4e2d\u53d1\u751f\u9519\u8bef\u65f6\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0bLaravel\u4f1a\u663e\u793a\u4e00\u4e2a\u7b80\u5355\u7684\u9519\u8bef\u754c\u9762\u3002\u4f46\u662f\u6211\u4eec\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\uff0c\u4ee5\u8fbe\u5230\u66f4\u4f18\u7f8e\u548c\u81ea\u5df1\u9700\u8981\u7684\u6548\u679c\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u81ea\u5df1\u7684\u89c6\u56fe\u6587\u4ef6\uff0c\u5e76\u5c06\u5b83\u4eec\u653e\u5728resources\/views\/errors\u76ee\u5f55\u4e0b\u3002\u7136\u540e\u5728app\/Exceptions\/Handler.php\u6587\u4ef6\u4e2d\u91cd\u5199render\u65b9\u6cd5\uff0c\u5f53\u53d1\u751f\u5f02\u5e38\u65f6\u663e\u793a\u6211\u4eec\u81ea\u5b9a\u4e49\u7684\u89c6\u56fe\u9875\u9762\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>public function render($request, Exception $exception)\n{\n    if ($this-&amp;gt;isHttpException($exception)) {\n        if ($exception-&amp;gt;getStatusCode() == 404) {\n            return response()-&amp;gt;view('errors.404', [], 404);\n        }\n    }\n\n    return parent::render($request, $exception);\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a404\u9519\u8bef\u9875\u9762\uff0c\u5e76\u5728render\u65b9\u6cd5\u4e2d\u5224\u65ad\u72b6\u6001\u7801\u662f\u5426\u4e3a404\u3002\u5982\u679c\u662f\uff0c\u5c31\u8fd4\u56de\u6211\u4eec\u521a\u521a\u521b\u5efa\u7684\u89c6\u56fe\u9875\u9762\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>Laravel\u63d0\u4f9b\u4e86\u5f88\u591a\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u4f7f\u5f00\u53d1\u4eba\u5458\u66f4\u8f7b\u677e\u5730\u5904\u7406\u9519\u8bef\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u6587\u4ef6\u3001\u5f02\u5e38\u5904\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u548c\u81ea\u5b9a\u4e49\u9519\u8bef\u9875\u9762\u7b49\u65b9\u5f0f\u6765\u638c\u63a7\u7a0b\u5e8f\u4e2d\u6240\u6709\u9519\u8bef\u3002<\/p>\n<p>\u5f53\u7136\uff0c\u4f18\u79c0\u7684\u7a0b\u5e8f\u5458\u8981\u5c3d\u53ef\u80fd\u51cf\u5c11\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef\u51fa\u73b0\uff0c\u907f\u514d\u5728\u540e\u671f\u624d\u53bb\u5904\u7406\u548c\u89e3\u51b3\u9519\u8bef\uff0c\u56e0\u4e3a\u5c3d\u53ef\u80fd\u51cf\u5c11\u9519\u8bef\u7684\u51fa\u73b0\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u6027\u80fd\uff0c\u8fd8\u53ef\u4ee5\u5927\u5927\u51cf\u5c11\u540e\u671f\u7ef4\u62a4\u7684\u7cbe\u529b\u6210\u672c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662flaravel \u9519\u8bef\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\u4e2a\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f88\u591a\u9519\u8bef\u5904\u7406\u673a\u5236\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u8f7b\u677e\u5730\u8c03\u8bd5\u548c\u5904\u7406\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8laravel\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u3002 \u914d\u7f6e\u6587\u4ef6 Laravel\u7684\u914d\u7f6e\u6587\u4ef6config\/app.php\u4e2d\u6709\u4e00\u4e2akey\u503c\u4e3adebug\u7684\u9009\u9879\u3002\u5f53debug\u4e3atrue\u65f6\uff0cLaravel\u4f1a\u663e\u793a\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u4fbf\u4e8e\u5f00\u53d1\u4eba\u5458\u8c03\u8bd5\u7a0b\u5e8f\u3002\u5f53\u8bbe\u7f6e\u4e3afalse\u65f6\uff0c\u5219\u53ea\u663e\u793a\u4e00\u822c\u7684\u9519\u8bef\u4fe1\u606f\u3002 \u5f02\u5e38\u5904\u7406 Laravel\u4e2d\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u6765\u6355\u83b7\u4ee3\u7801\u4e2d\u7684\u9519\u8bef\u3002\u5f53\u7a0b\u5e8f\u4e2d\u53d1\u751f\u9519\u8bef\u65f6\uff0cLaravel\u4f1a\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff0c\u8fd9\u4e2a\u5f02\u5e38\u5c06\u88ab\u7cfb\u7edf\u5904\u7406\u3002 \u6211\u4eec\u53ef\u4ee5\u5728app\/Exceptions\/Handler.php\u6587\u4ef6\u4e2d\u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u5f02\u5e38\u3002\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff0c\u6709\u4e00\u4e2a\u65b9\u6cd5\u53eb\u505arender\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u88ab\u81ea\u52a8\u8c03\u7528\u4ee5\u6e32\u67d3\u5f02\u5e38\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u5982\u4f55\u5904\u7406\u5f02\u5e38\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a public function render($request, Exception $exception) { if ($exception instanceof IlluminateDatabaseEloquentModelNotFoundException) { return response()-&amp;gt;json([ &#8216;error&#8217; =&amp;gt; &#8216;Resource not found&#8217; ], 404); } return parent::render($request, $exception); } \u767b\u5f55\u540e\u590d\u5236 \u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5224\u65ad\u4e86\u5f02\u5e38\u7c7b\u578b\uff0c\u5982\u679c\u662fModelNotFoundException\uff0c\u5c31\u8fd4\u56de\u4e00\u4e2a404\u9519\u8bef\u548c\u4e00\u4e2ajson\u683c\u5f0f\u7684\u54cd\u5e94\uff0c\u544a\u8bc9\u5ba2\u6237\u7aef\u8d44\u6e90\u672a\u627e\u5230\u3002\u5982\u679c\u4e0d\u662f\u8fd9\u4e2a\u5f02\u5e38\u7c7b\u578b\uff0c\u5c31\u8c03\u7528\u7236\u7c7b\u7684render\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\u3002 \u65e5\u5fd7\u8bb0\u5f55 \u9664\u4e86\u5728\u63a7\u5236\u53f0\u6216\u8005\u6d4f\u89c8\u5668\u4e0a\u663e\u793a\u9519\u8bef\u4fe1\u606f\uff0cLaravel\u8fd8\u652f\u6301\u5c06\u9519\u8bef\u4fe1\u606f\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8ffd\u8e2a\u548c\u8c03\u8bd5\u7a0b\u5e8f\u4e2d\u7684\u9519\u8bef\u3002 Laravel\u9ed8\u8ba4\u4f7f\u7528Monolog\u5e93\u6765\u8bb0\u5f55\u65e5\u5fd7\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4fee\u6539\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u65e5\u5fd7\u9009\u9879\u6765\u8bbe\u7f6e\u65e5\u5fd7\u8bb0\u5f55\u7ea7\u522b\u548c\u65e5\u5fd7\u6587\u4ef6\u4f4d\u7f6e\u3002 &#8216;log&#8217; =&amp;gt; env(&#8216;APP_LOG&#8217;, &#8216;single&#8217;), &#8216;log_level&#8217; =&amp;gt; env(&#8216;APP_LOG_LEVEL&#8217;, &#8216;debug&#8217;), &#8216;channels&#8217; =&amp;gt; [ &#8216;stack&#8217; =&amp;gt; [ &#8216;driver&#8217; =&amp;gt; &#8216;stack&#8217;, &#8216;channels&#8217; =&amp;gt; [&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-22835","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22835","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=22835"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22835\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}