{"id":23053,"date":"2024-11-21T11:42:13","date_gmt":"2024-11-21T03:42:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23053\/"},"modified":"2024-11-21T11:42:13","modified_gmt":"2024-11-21T03:42:13","slug":"thinkphp5-0%e5%a6%82%e4%bd%95%e9%85%8d%e7%bd%ae%e9%94%99%e8%af%af%e9%a1%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23053\/","title":{"rendered":"thinkphp5.0\u5982\u4f55\u914d\u7f6e\u9519\u8bef\u9875"},"content":{"rendered":"<\/p>\n<p><strong>I. ThinkPHP 5.0 \u9519\u8bef\u9875\u7684\u4f5c\u7528<\/strong><\/p>\n<ul>\n<li>\n<p>\u9519\u8bef\u9875\u9762\u4e3b\u8981\u7528\u4e8e\u6355\u83b7\u5e94\u7528\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u51fa\u73b0\u7684\u9519\u8bef\u5e76\u63d0\u4f9b\u8bbf\u95ee\u9519\u8bef\u65e5\u5fd7\u7684\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<li>\n<p>\u9519\u8bef\u9875\u9762\u8fd8\u652f\u6301\u5b9e\u65f6\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\uff0c\u53ef\u4ee5\u5feb\u901f\u6392\u67e5\u548c\u89e3\u51b3\u751f\u4ea7\u73af\u5883\u4e2d\u7684\u9519\u8bef\u3002<\/p>\n<\/li>\n<\/ul>\n<p><strong>II. \u914d\u7f6e\u9519\u8bef\u9875<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u914d\u7f6e\u9519\u8bef\u9875\u9762\u9700\u8981\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u8fdb\u884c\uff0c\u4f8b\u5982\u5728 config.php \u6587\u4ef6\u4e2d\u589e\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<pre>'exception_handle'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&amp;gt;&nbsp;'appindexexceptionHttp',<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d appindexexceptionHttp \u662f\u6307\u5f02\u5e38\u5904\u7406\u7c7b\u6240\u5728\u7684\u547d\u540d\u7a7a\u95f4\u548c\u7c7b\u540d\u3002\u5f02\u5e38\u5904\u7406\u7c7b\u9700\u8981\u7ee7\u627f thinkexceptionHandle \u7c7b\uff0c\u5e76\u91cd\u5199 render \u65b9\u6cd5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u5f02\u5e38\u4fe1\u606f\u7684\u8f93\u51fa\u3002<\/p>\n<p><strong>III. \u9519\u8bef\u9875\u7684\u9ed8\u8ba4\u8bbe\u7f6e<\/strong><\/p>\n<p>ThinkPHP 5.0 \u4e2d\u7684\u9ed8\u8ba4\u9519\u8bef\u9875\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<ol>\n<li>\n<p>\u5f02\u5e38\u7c7b\u540d<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u9519\u8bef\u7801<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u9519\u8bef\u63cf\u8ff0<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u9519\u8bef\u6587\u4ef6\u53ca\u884c\u53f7<\/p>\n<\/li>\n<li>\n<p>\u5f02\u5e38\u8ffd\u6eaf\u4fe1\u606f<\/p>\n<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u4fe1\u606f\u53ef\u5e2e\u52a9\u5feb\u901f\u5b9a\u4f4d\u9519\u8bef\u4f4d\u7f6e\u5e76\u8fdb\u884c\u8ddf\u8e2a\u5206\u6790\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u9519\u8bef\u9875\u8fd8\u63d0\u4f9b\u4e86\u64cd\u4f5c\u6309\u94ae\uff0c\u4ee5\u4fbf\u5f00\u53d1\u8005\u8fdb\u884c\u4e00\u4e9b\u5e38\u7528\u64cd\u4f5c\u3002<\/p>\n<p><strong>IV. \u81ea\u5b9a\u4e49\u9519\u8bef\u9875<\/strong><\/p>\n<p>\u9519\u8bef\u9875\u8fd8\u652f\u6301\u81ea\u5b9a\u4e49\uff0c\u53ea\u9700\u5728\u63a7\u5236\u5668\u4e2d\u7ee7\u627f thinkexceptionHandle \u7c7b\uff0c\u5e76\u91cd\u5199 render \u65b9\u6cd5\u5373\u53ef\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>namespace&nbsp;appindexexception;\n\nuse&nbsp;thinkexceptionHandle;\n\nclass&nbsp;Http&nbsp;extends&nbsp;Handle\n{\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;render(Exception&nbsp;$e)\n&nbsp;&nbsp;&nbsp;&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($e&nbsp;instanceof&nbsp;HttpException)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$status&nbsp;=&nbsp;$e-&amp;gt;getStatusCode();\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$status&nbsp;=&nbsp;404;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data&nbsp;=&nbsp;[\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'status'&nbsp;=&amp;gt;&nbsp;$status,\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'message'&nbsp;=&amp;gt;&nbsp;$this-&amp;gt;getMessage($e),\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'exception'&nbsp;=&amp;gt;&nbsp;$this-&amp;gt;isDebug()&nbsp;?&nbsp;$this-&amp;gt;getTrace($e)&nbsp;:&nbsp;[],\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;];\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;json($data);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u81ea\u5b9a\u4e49\u5f02\u5e38\u4fe1\u606f\uff0c\u5728\u53d1\u751f\u9519\u8bef\u65f6\u8fd4\u56de JSON \u5bf9\u8c61\u683c\u5f0f\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f5.0\u5982\u4f55\u914d\u7f6e\u9519\u8bef\u9875\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>I. ThinkPHP 5.0 \u9519\u8bef\u9875\u7684\u4f5c\u7528 \u9519\u8bef\u9875\u9762\u4e3b\u8981\u7528\u4e8e\u6355\u83b7\u5e94\u7528\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u51fa\u73b0\u7684\u9519\u8bef\u5e76\u63d0\u4f9b\u8bbf\u95ee\u9519\u8bef\u65e5\u5fd7\u7684\u65b9\u6cd5\u3002 \u9519\u8bef\u9875\u9762\u8fd8\u652f\u6301\u5b9e\u65f6\u8bb0\u5f55\u9519\u8bef\u4fe1\u606f\uff0c\u53ef\u4ee5\u5feb\u901f\u6392\u67e5\u548c\u89e3\u51b3\u751f\u4ea7\u73af\u5883\u4e2d\u7684\u9519\u8bef\u3002 II. \u914d\u7f6e\u9519\u8bef\u9875 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u914d\u7f6e\u9519\u8bef\u9875\u9762\u9700\u8981\u5728\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u6587\u4ef6\u4e2d\u8fdb\u884c\uff0c\u4f8b\u5982\u5728 config.php \u6587\u4ef6\u4e2d\u589e\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a &#8216;exception_handle&#8217;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=&amp;gt;&nbsp;&#8216;appindexexceptionHttp&#8217;, \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d appindexexceptionHttp \u662f\u6307\u5f02\u5e38\u5904\u7406\u7c7b\u6240\u5728\u7684\u547d\u540d\u7a7a\u95f4\u548c\u7c7b\u540d\u3002\u5f02\u5e38\u5904\u7406\u7c7b\u9700\u8981\u7ee7\u627f thinkexceptionHandle \u7c7b\uff0c\u5e76\u91cd\u5199 render \u65b9\u6cd5\u5b9e\u73b0\u81ea\u5b9a\u4e49\u5f02\u5e38\u4fe1\u606f\u7684\u8f93\u51fa\u3002 III. \u9519\u8bef\u9875\u7684\u9ed8\u8ba4\u8bbe\u7f6e ThinkPHP 5.0 \u4e2d\u7684\u9ed8\u8ba4\u9519\u8bef\u9875\u5305\u542b\u4ee5\u4e0b\u5185\u5bb9\uff1a \u5f02\u5e38\u7c7b\u540d \u5f02\u5e38\u9519\u8bef\u7801 \u5f02\u5e38\u9519\u8bef\u63cf\u8ff0 \u5f02\u5e38\u9519\u8bef\u6587\u4ef6\u53ca\u884c\u53f7 \u5f02\u5e38\u8ffd\u6eaf\u4fe1\u606f \u4ee5\u4e0a\u4fe1\u606f\u53ef\u5e2e\u52a9\u5feb\u901f\u5b9a\u4f4d\u9519\u8bef\u4f4d\u7f6e\u5e76\u8fdb\u884c\u8ddf\u8e2a\u5206\u6790\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u9519\u8bef\u9875\u8fd8\u63d0\u4f9b\u4e86\u64cd\u4f5c\u6309\u94ae\uff0c\u4ee5\u4fbf\u5f00\u53d1\u8005\u8fdb\u884c\u4e00\u4e9b\u5e38\u7528\u64cd\u4f5c\u3002 IV. \u81ea\u5b9a\u4e49\u9519\u8bef\u9875 \u9519\u8bef\u9875\u8fd8\u652f\u6301\u81ea\u5b9a\u4e49\uff0c\u53ea\u9700\u5728\u63a7\u5236\u5668\u4e2d\u7ee7\u627f thinkexceptionHandle \u7c7b\uff0c\u5e76\u91cd\u5199 render \u65b9\u6cd5\u5373\u53ef\uff0c\u4f8b\u5982\uff1a namespace&nbsp;appindexexception; use&nbsp;thinkexceptionHandle; class&nbsp;Http&nbsp;extends&nbsp;Handle { &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;render(Exception&nbsp;$e) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;($e&nbsp;instanceof&nbsp;HttpException)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$status&nbsp;=&nbsp;$e-&amp;gt;getStatusCode(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$status&nbsp;=&nbsp;404; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data&nbsp;=&nbsp;[ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8216;status&#8217;&nbsp;=&amp;gt;&nbsp;$status, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8216;message&#8217;&nbsp;=&amp;gt;&nbsp;$this-&amp;gt;getMessage($e), &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8216;exception&#8217;&nbsp;=&amp;gt;&nbsp;$this-&amp;gt;isDebug()&nbsp;?&nbsp;$this-&amp;gt;getTrace($e)&nbsp;:&nbsp;[], &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;]; [&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-23053","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23053","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=23053"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23053\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23053"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23053"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23053"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}