{"id":23647,"date":"2024-11-21T09:15:03","date_gmt":"2024-11-21T01:15:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23647\/"},"modified":"2024-11-21T09:15:03","modified_gmt":"2024-11-21T01:15:03","slug":"%e5%88%a9%e7%94%a8thinkphp6%e5%ae%9e%e7%8e%b0%e6%bc%82%e4%ba%ae%e7%9a%84404%e9%a1%b5%e9%9d%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23647\/","title":{"rendered":"\u5229\u7528ThinkPHP6\u5b9e\u73b0\u6f02\u4eae\u7684404\u9875\u9762"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u65e5\u76ca\u53d1\u5c55\uff0c\u8bb8\u591a\u7f51\u7ad9\u6216\u5e94\u7528\u4e5f\u9010\u6e10\u53d8\u5f97\u590d\u6742\u3002\u5f53\u7528\u6237\u5728\u4f7f\u7528\u65f6\uff0c\u65f6\u5e38\u4f1a\u9047\u5230\u9519\u8bef\u9875\u9762\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u5c31\u662f404\u9875\u9762\u3002404\u9875\u9762\u6307\u8bbf\u95ee\u7684\u9875\u9762\u4e0d\u5b58\u5728\uff0c\u662f\u5e38\u89c1\u7684\u9519\u8bef\u9875\u9762\u3002\u800c\u5bf9\u4e8e\u7f51\u7ad9\u6216\u5e94\u7528\u6765\u8bf4\uff0c\u4e00\u4e2a\u6f02\u4eae\u7684404\u9875\u9762\u80fd\u6781\u5927\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u4ecb\u7ecd\u5982\u4f55\u5229\u75286\u5feb\u901f\u5b9e\u73b0\u4e00\u4e2a\u6f02\u4eae\u7684404\u9875\u9762\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u8def\u7531<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728route\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2aerror.php\u6587\u4ef6\uff0c\u5728\u5176\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u8def\u7531\uff0c\u5c06\u9519\u8bef\u9875\u9762\u6307\u5411\u6211\u4eec\u65b0\u5efa\u7684Error\u63a7\u5236\u5668\u4e2d\u7684index\u65b9\u6cd5\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>acadeRoute;\n\nRoute::rule('\/404', 'error\/index')-&amp;gt;name('404');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u6211\u4eec\u5c06\u8def\u7531\u540d\u79f0\u4e3a404\uff0c\u65b9\u4fbf\u5728\u540e\u9762\u8c03\u7528\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u63a7\u5236\u5668\u548c\u89c6\u56fe<\/li>\n<\/ol>\n<p>\u5728\u63a7\u5236\u5668\u6587\u4ef6\u5939\u4e2d\u65b0\u5efaError.php\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace appindexcontroller;\n\nuse thinkController;\n\nclass Error extends Controller\n{\n    public function index()\n    {\n        return $this-&gt;fetch('error\/404');\n    }\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u7ee7\u627fController\u7c7b\uff0c\u5e76\u5728index\u65b9\u6cd5\u4e2d\u8fd4\u56de\u65b0\u5efa\u7684404\u89c6\u56fe\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u5728\u89c6\u56fe\u6587\u4ef6\u5939\u4e2d\u65b0\u5efaerror\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efa404.html\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>\n\n\n    &lt;meta charset=\"UTF-8\"&gt;&lt;title&gt;404 Not Found&lt;\/title&gt;&lt;style&gt;\n        body {\n            background-color: #eee;\n            text-align: center;\n            font-family: 'Microsoft YaHei', sans-serif;\n        }\n        .container {\n            margin-top: 10%;\n        }\n        h1 {\n            font-size: 8em;\n            color: #ddd;\n            margin-bottom: 0;\n        }\n        p {\n            font-size: 3em;\n            color: #777;\n            margin-top: 0;\n        }\n        a {\n            text-decoration: none;\n            color: #000;\n            border: 1px solid #000;\n            padding: 10px 20px;\n            margin-top: 30px;\n            display: inline-block;\n        }\n        a:hover {\n            border-color: #777;\n        }\n    &lt;\/style&gt;&lt;div class=\"container\"&gt;\n        &lt;h1&gt;404&lt;\/h1&gt;\n        &lt;p&gt;Page not found&lt;\/p&gt;\n        &lt;a href=\"&amp;lt;?php%20echo%20url('\/')%20?&amp;gt;\"&gt;Go back home&lt;\/a&gt;\n    &lt;\/div&gt;\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u91c7\u7528\u4e86\u7b80\u6d01\u7f8e\u89c2\u7684\u8bbe\u8ba1\uff0c\u5c55\u793a\u4e86404\u9875\u9762\u7684\u4e3b\u9898\u548c\u6309\u94ae\u3002<\/p>\n<ol>\n<li>\u6d4b\u8bd5<\/li>\n<\/ol>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u968f\u610f\u8bbf\u95ee\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u8def\u7531\uff0c\u4f8b\u5982\uff0c\u8bbf\u95eehttp:\/\/yourdomain.com\/abc\uff0c\u5c31\u80fd\u770b\u5230\u6211\u4eec\u65b0\u5efa\u7684\u6f02\u4eae404\u9875\u9762\u3002<\/p>\n<ol>\n<li>\u81ea\u5b9a\u4e49404\u9875\u9762<\/li>\n<\/ol>\n<p>\u5982\u679c\u4f60\u60f3\u52a0\u5165\u66f4\u591a\u7684\u5185\u5bb9\u6216\u8c03\u6574404\u9875\u9762\u7684\u6837\u5f0f\uff0c\u4f60\u53ea\u9700\u8981\u4fee\u6539\u89c6\u56fe\u6587\u4ef6\u5373\u53ef\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u5b9a\u5236404\u9875\u9762\uff0c\u6bd4\u5982\u52a0\u5165\u641c\u7d22\u529f\u80fd\u3001\u8054\u7cfb\u65b9\u5f0f\u7b49\u7b49\u3002\u540c\u65f6\uff0c\u5982\u679c\u4f60\u62c5\u5fc3\u6d4f\u89c8\u5668\u7f13\u5b58\u5f71\u54cd\u5c55\u793a\u6548\u679c\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u89c6\u56fe\u9875\u9762\u5f15\u5165CSS\u548cJS\u6587\u4ef6\u7684\u65b9\u5f0f\u89e3\u51b3\u3002<\/p>\n<ol>\n<li>\u603b\u7ed3<\/li>\n<\/ol>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528ThinkPHP6\u5b9e\u73b0\u4e00\u4e2a\u6f02\u4eae\u7684404\u9875\u9762\u3002\u901a\u8fc7\u521b\u5efa\u8def\u7531\u3001\u63a7\u5236\u5668\u548c\u89c6\u56fe\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u6784\u5efa\u4e00\u4e2a\u7b26\u5408\u81ea\u8eab\u9700\u6c42\u7684404\u9875\u9762\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c404\u9875\u9762\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u7528\u6237\u4f53\u9a8c\u56e0\u7d20\uff0c\u6211\u4eec\u5e94\u8be5\u6ce8\u91cd\u5176\u8bbe\u8ba1\u548c\u7f8e\u5316\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5229\u7528ThinkPHP6\u5b9e\u73b0\u6f02\u4eae\u7684404\u9875\u9762\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>\u968f\u7740\u4e92\u8054\u7f51\u7684\u65e5\u76ca\u53d1\u5c55\uff0c\u8bb8\u591a\u7f51\u7ad9\u6216\u5e94\u7528\u4e5f\u9010\u6e10\u53d8\u5f97\u590d\u6742\u3002\u5f53\u7528\u6237\u5728\u4f7f\u7528\u65f6\uff0c\u65f6\u5e38\u4f1a\u9047\u5230\u9519\u8bef\u9875\u9762\uff0c\u5176\u4e2d\u6700\u5e38\u89c1\u7684\u5c31\u662f404\u9875\u9762\u3002404\u9875\u9762\u6307\u8bbf\u95ee\u7684\u9875\u9762\u4e0d\u5b58\u5728\uff0c\u662f\u5e38\u89c1\u7684\u9519\u8bef\u9875\u9762\u3002\u800c\u5bf9\u4e8e\u7f51\u7ad9\u6216\u5e94\u7528\u6765\u8bf4\uff0c\u4e00\u4e2a\u6f02\u4eae\u7684404\u9875\u9762\u80fd\u6781\u5927\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u4f1a\u4ecb\u7ecd\u5982\u4f55\u5229\u75286\u5feb\u901f\u5b9e\u73b0\u4e00\u4e2a\u6f02\u4eae\u7684404\u9875\u9762\u3002 \u521b\u5efa\u8def\u7531 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728route\u6587\u4ef6\u5939\u4e2d\u521b\u5efa\u4e00\u4e2aerror.php\u6587\u4ef6\uff0c\u5728\u5176\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u8def\u7531\uff0c\u5c06\u9519\u8bef\u9875\u9762\u6307\u5411\u6211\u4eec\u65b0\u5efa\u7684Error\u63a7\u5236\u5668\u4e2d\u7684index\u65b9\u6cd5\u3002\u4ee3\u7801\u5982\u4e0b\uff1a acadeRoute; Route::rule(&#8216;\/404&#8217;, &#8216;error\/index&#8217;)-&amp;gt;name(&#8216;404&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u6211\u4eec\u5c06\u8def\u7531\u540d\u79f0\u4e3a404\uff0c\u65b9\u4fbf\u5728\u540e\u9762\u8c03\u7528\u3002 \u521b\u5efa\u63a7\u5236\u5668\u548c\u89c6\u56fe \u5728\u63a7\u5236\u5668\u6587\u4ef6\u5939\u4e2d\u65b0\u5efaError.php\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php namespace appindexcontroller; use thinkController; class Error extends Controller { public function index() { return $this-&gt;fetch(&#8216;error\/404&#8217;); } } \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u91cc\u6211\u4eec\u7ee7\u627fController\u7c7b\uff0c\u5e76\u5728index\u65b9\u6cd5\u4e2d\u8fd4\u56de\u65b0\u5efa\u7684404\u89c6\u56fe\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u5728\u89c6\u56fe\u6587\u4ef6\u5939\u4e2d\u65b0\u5efaerror\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efa404.html\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a &lt;meta charset=&#8221;UTF-8&#8243;&gt;&lt;title&gt;404 Not Found&lt;\/title&gt;&lt;style&gt; body { background-color: #eee; text-align: center; font-family: &#8216;Microsoft YaHei&#8217;, sans-serif; } .container { margin-top: 10%; } h1 { font-size: [&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-23647","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23647","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=23647"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23647\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}