{"id":23192,"date":"2024-11-21T10:46:08","date_gmt":"2024-11-21T02:46:08","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23192\/"},"modified":"2024-11-21T10:46:08","modified_gmt":"2024-11-21T02:46:08","slug":"thinkphp%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e5%8d%95%e9%a1%b5","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23192\/","title":{"rendered":"thinkphp\u5982\u4f55\u8bbe\u7f6e\u5355\u9875"},"content":{"rendered":"<\/p>\n<p><strong>\u4e00\u3001\u4ec0\u4e48\u662f\u5355\u9875\u5e94\u7528<\/strong><\/p>\n<p>\u5355\u9875\u5e94\u7528\uff0c\u82f1\u6587\u4e3aSingle-Page Application\uff0c\u7b80\u79f0SPA\uff0c\u5b83\u662f\u4e00\u79cd\u57fa\u4e8eAjax\u548cHTML5\u7b49\u6280\u672f\uff0c\u901a\u8fc7\u52a8\u6001\u7684\u52a0\u8f7d\u9875\u9762\u5c40\u90e8\u5185\u5bb9\u6765\u4f18\u5316\u7528\u6237\u4f53\u9a8c\u7684web\u5e94\u7528\u7a0b\u5e8f\u3002\u4e0e\u4f20\u7edf\u7684\u5e94\u7528\u7a0b\u5e8f\u4e0d\u540c\uff0c\u5355\u9875\u5e94\u7528\u53ea\u5305\u542b\u4e00\u4e2aHTML\u9875\u9762\uff0c\u901a\u8fc7JavaScript\u5c06\u9875\u9762\u5185\u5bb9\u52a8\u6001\u8f7d\u5165\uff0c\u4ece\u800c\u5b9e\u73b0\u5185\u5bb9\u7684\u5448\u73b0\u548c\u9875\u9762\u95f4\u7684\u5207\u6362\u3002<\/p>\n<p><strong>\u4e8c\u3001\u521b\u5efa\u5355\u9875\u5e94\u7528<\/strong><\/p>\n<p>\u5728ThinkPHP\u4e2d\u521b\u5efa\u5355\u9875\u5e94\u7528\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>1.\u521b\u5efa\u63a7\u5236\u5668<\/p>\n<p>\u5728ThinkPHP\u4e2d\uff0c\u6240\u6709\u7684\u4e1a\u52a1\u903b\u8f91\u90fd\u662f\u5728\u63a7\u5236\u5668\u4e2d\u5b9e\u73b0\u7684\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5728\u5e94\u7528\u7684\u63a7\u5236\u5668\u4e2d\u521b\u5efa\u5355\u9875\u5e94\u7528\u7684\u63a7\u5236\u5668\u3002\u6211\u4eec\u5efa\u7acb\u4e00\u4e2a\u63a7\u5236\u5668\uff0c\u547d\u540d\u4e3aSinglePageController\u3002<\/p>\n<pre>&lt;?php namespace appindexcontroller;\nuse thinkController;\nclass SinglePageController extends Controller\n{\n    public function index()\n    {\n        return view();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aSinglePageController\u7c7b\u7ee7\u627f\u81eaController\u7c7b\uff0c\u5e76\u4e14\u5b9a\u4e49\u4e86\u4e00\u4e2aindex\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u6267\u884c\u9875\u9762\u52a0\u8f7d\u64cd\u4f5c\u3002<\/p>\n<p>2.\u521b\u5efa\u89c6\u56fe<\/p>\n<p>\u6211\u4eec\u9700\u8981\u901a\u8fc7ThinkPHP\u7684\u89c6\u56fe\u5f15\u64ce\u6765\u521b\u5efa\u4e00\u4e2a\u5355\u9875\u5e94\u7528\u7684\u89c6\u56fe\u3002\u5229\u7528ThinkPHP\u7684\u89c6\u56fe\u5f15\u64ce\uff0c\u6211\u4eec\u53ef\u4ee5\u5728HTML\u4ee3\u7801\u4e2d\u52a0\u5165PHP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&nbsp;&nbsp;&nbsp;&lt;div&gt;&lt;\/div&gt;\n&nbsp;&nbsp;&nbsp;&nbsp;&lt;script&gt;\n        $(document).ready(function(){\n            $(window).bind(&#039;hashchange&#039;, function() {\n                var url = window.location.hash.slice(1);\n                $(&#039;#content&#039;).load(url + &#039;.html&#039;);\n            });\n            $(window).trigger(&#039;hashchange&#039;);\n        });\n    &lt;\/script&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6211\u4eec\u5728\u4ee3\u7801\u4e2d\u521b\u5efa\u4e86\u4e00\u4e2a\u5bfc\u822a\u6761\uff0c\u5b83\u7684id\u4e3anav\uff0c\u4ee5\u53ca\u4e00\u4e2a\u5185\u5bb9\u533a\u57df\uff0c\u5b83\u7684id\u4e3acontent\u3002\u5e76\u4e14\u6211\u4eec\u5728JavaScript\u4e2d\u7ed1\u5b9a\u4e86hashchange\u4e8b\u4ef6\uff0c\u6bcf\u5f53hash\u53d1\u751f\u6539\u53d8\u65f6\uff08\u5373\u5bfc\u822a\u6761\u94fe\u63a5\u6539\u53d8\u65f6\uff09\uff0c\u6211\u4eec\u5c06\u901a\u8fc7Ajax\u52a0\u8f7d\u5bf9\u5e94\u7684HTML\u9875\u9762\u5230\u5185\u5bb9\u533a\u57df\u4e2d\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u5355\u9875\u5e94\u7528\u7684\u5185\u5bb9\u5e94\u8be5\u662f\u4ee5\u591a\u4e2aHTML\u9875\u9762\u7ec4\u6210\u7684\uff0c\u800c\u4e0d\u662f\u5728\u4e00\u4e2a\u9875\u9762\u5185\u540c\u65f6\u5168\u90e8\u5c55\u793a\u3002<\/p>\n<p>3.\u5b9a\u4e49\u8def\u7531\u89c4\u5219<\/p>\n<p>\u7531\u4e8e\u6211\u4eec\u7684SinglePageController\u7c7b\u662f\u901a\u8fc7\u7d22\u5f15\u65b9\u6cd5\u6765\u6e32\u67d3\u5355\u9875\u5e94\u7528\u7684\u89c6\u56fe\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u9700\u8981\u5728\u8def\u7531\u89c4\u5219\u4e2d\u5b9a\u4e49\u540d\u4e3asinglepage\u7684\u8def\u7531\u89c4\u5219\uff1a<\/p>\n<pre>&lt;?php use thinkRoute;\n\/\/ \u5355\u9875\u5e94\u7528\u8def\u7531\nRoute::rule(&#039;singlepage\/:id&#039;,&#039;index\/SinglePage\/index&#039;);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3asinglepage\u7684\u8def\u7531\u89c4\u5219\uff0c\u5c06id\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9SinglePageController\u7c7b\u7684index\u65b9\u6cd5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u8bbe\u7f6e\u5355\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>\u4e00\u3001\u4ec0\u4e48\u662f\u5355\u9875\u5e94\u7528 \u5355\u9875\u5e94\u7528\uff0c\u82f1\u6587\u4e3aSingle-Page Application\uff0c\u7b80\u79f0SPA\uff0c\u5b83\u662f\u4e00\u79cd\u57fa\u4e8eAjax\u548cHTML5\u7b49\u6280\u672f\uff0c\u901a\u8fc7\u52a8\u6001\u7684\u52a0\u8f7d\u9875\u9762\u5c40\u90e8\u5185\u5bb9\u6765\u4f18\u5316\u7528\u6237\u4f53\u9a8c\u7684web\u5e94\u7528\u7a0b\u5e8f\u3002\u4e0e\u4f20\u7edf\u7684\u5e94\u7528\u7a0b\u5e8f\u4e0d\u540c\uff0c\u5355\u9875\u5e94\u7528\u53ea\u5305\u542b\u4e00\u4e2aHTML\u9875\u9762\uff0c\u901a\u8fc7JavaScript\u5c06\u9875\u9762\u5185\u5bb9\u52a8\u6001\u8f7d\u5165\uff0c\u4ece\u800c\u5b9e\u73b0\u5185\u5bb9\u7684\u5448\u73b0\u548c\u9875\u9762\u95f4\u7684\u5207\u6362\u3002 \u4e8c\u3001\u521b\u5efa\u5355\u9875\u5e94\u7528 \u5728ThinkPHP\u4e2d\u521b\u5efa\u5355\u9875\u5e94\u7528\u6709\u5f88\u591a\u79cd\u65b9\u6cd5\uff0c\u672c\u6587\u5c06\u4ecb\u7ecd\u5176\u4e2d\u7684\u4e00\u79cd\u65b9\u6cd5\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b 1.\u521b\u5efa\u63a7\u5236\u5668 \u5728ThinkPHP\u4e2d\uff0c\u6240\u6709\u7684\u4e1a\u52a1\u903b\u8f91\u90fd\u662f\u5728\u63a7\u5236\u5668\u4e2d\u5b9e\u73b0\u7684\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5728\u5e94\u7528\u7684\u63a7\u5236\u5668\u4e2d\u521b\u5efa\u5355\u9875\u5e94\u7528\u7684\u63a7\u5236\u5668\u3002\u6211\u4eec\u5efa\u7acb\u4e00\u4e2a\u63a7\u5236\u5668\uff0c\u547d\u540d\u4e3aSinglePageController\u3002 &lt;?php namespace appindexcontroller; use thinkController; class SinglePageController extends Controller { public function index() { return view(); } } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aSinglePageController\u7c7b\u7ee7\u627f\u81eaController\u7c7b\uff0c\u5e76\u4e14\u5b9a\u4e49\u4e86\u4e00\u4e2aindex\u65b9\u6cd5\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5c06\u6267\u884c\u9875\u9762\u52a0\u8f7d\u64cd\u4f5c\u3002 2.\u521b\u5efa\u89c6\u56fe \u6211\u4eec\u9700\u8981\u901a\u8fc7ThinkPHP\u7684\u89c6\u56fe\u5f15\u64ce\u6765\u521b\u5efa\u4e00\u4e2a\u5355\u9875\u5e94\u7528\u7684\u89c6\u56fe\u3002\u5229\u7528ThinkPHP\u7684\u89c6\u56fe\u5f15\u64ce\uff0c\u6211\u4eec\u53ef\u4ee5\u5728HTML\u4ee3\u7801\u4e2d\u52a0\u5165PHP\u4ee3\u7801\uff0c\u4ece\u800c\u5b9e\u73b0\u89c6\u56fe\u7684\u91cd\u7528\u4ee5\u53ca\u9875\u9762\u5206\u79bb\u7b49\u529f\u80fd\u3002 &lt;!&#8211; singlepage.html &#8211;&gt; nbsp;html&amp;gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;meta&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;title&gt;\u5355\u9875\u5e94\u7528&lt;\/title&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;script&gt;&lt;\/script&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;div&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;ul&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;&lt;a&gt;\u9996\u9875&lt;\/a&gt;&lt;\/li&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;&lt;a&gt;\u5173\u4e8e\u6211\u4eec&lt;\/a&gt;&lt;\/li&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;&lt;a&gt;\u4ea7\u54c1\u4ecb\u7ecd&lt;\/a&gt;&lt;\/li&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;li&gt;&lt;a&gt;\u8054\u7cfb\u6211\u4eec&lt;\/a&gt;&lt;\/li&gt; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;\/ul&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;\/div&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;div&gt;&lt;\/div&gt; &nbsp;&nbsp;&nbsp;&nbsp;&lt;script&gt; $(document).ready(function(){ $(window).bind(&#039;hashchange&#039;, function() { var url = window.location.hash.slice(1); [&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-23192","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23192","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=23192"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23192\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}