{"id":23649,"date":"2024-11-21T10:23:10","date_gmt":"2024-11-21T02:23:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23649\/"},"modified":"2024-11-21T10:23:10","modified_gmt":"2024-11-21T02:23:10","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8thinkphp6%e7%9a%84%e8%a7%86%e5%9b%be%e7%bb%84%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23649\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528ThinkPHP6\u7684\u89c6\u56fe\u7ec4\u4ef6"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u7f51\u7ad9\u548c\u5e94\u7528\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u8fd9\u5c31\u9700\u8981\u4e00\u4e2a\u9ad8\u6548\u7684\u6846\u67b6\u6765\u7f29\u77ed\u5f00\u53d1\u5468\u671f\u3002\u662f\u4e00\u4e2a\u9886\u5148\u7684php\u6846\u67b6\uff0c\u63d0\u4f9b\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u529f\u80fd\u6765\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5feb\u901f\u6784\u5efa\u9ad8\u8d28\u91cf\u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<p>ThinkPHP\u76846\u7248\u672c\u5f15\u5165\u4e86\u4e00\u4e2a\u5168\u65b0\u7684\u89c6\u56fe\u7ec4\u4ef6\uff0c\u4f7f\u5f97\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u66f4\u52a0\u8f7b\u677e\u5730\u6784\u5efa\u52a8\u6001\u7684\u7f51\u9875\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6613\u7528\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528ThinkPHP6\u7684\u89c6\u56fe\u7ec4\u4ef6\u3002<\/p>\n<ol>\n<li>\u6982\u8ff0<\/li>\n<\/ol>\n<p>\u89c6\u56fe\u662fMVC\u67b6\u6784\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u662f\u6307\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8d1f\u8d23\u5728\u7f51\u9875\u4e2d\u663e\u793a\u6570\u636e\u7684\u90e8\u5206\u3002ThinkPHP6\u7684\u89c6\u56fe\u7ec4\u4ef6\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5c06\u9875\u9762\u548c\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\u5206\u79bb\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<ol>\n<li>\u89c6\u56fe\u7684\u4f7f\u7528<\/li>\n<\/ol>\n<p>\u5728ThinkPHP6\u4e2d\uff0c\u89c6\u56fe\u6587\u4ef6\u5b58\u653e\u5728\/views\u76ee\u5f55\u4e0b\uff0c\u9ed8\u8ba4\u4e3a\/index.html\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528View\u7c7b\u6765\u6e32\u67d3\u89c6\u56fe\uff1a<\/p>\n<pre>use thinkacadeView;\n\nclass Index\n{\n    public function index()\n    {\n        return View::fetch('index');\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528View\u7c7b\u6765\u6e32\u67d3\u89c6\u56fe\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u89c6\u56fe\u7684\u7ee7\u627f\u548c\u5e03\u5c40<\/li>\n<\/ol>\n<p>\u89c6\u56fe\u7684\u7ee7\u627f\u548c\u5e03\u5c40\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u7684\u6280\u672f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u66f4\u6709\u6548\u5730\u7f16\u5199\u89c6\u56fe\u4ee3\u7801\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528layout\u65b9\u6cd5\u6765\u6307\u5b9a\u89c6\u56fe\u7684\u5e03\u5c40\uff1a<\/p>\n<pre>use thinkacadeView;\n\nclass Index\n{\n    public function index()\n    {\n        return View::fetch('index')-&amp;gt;layout('common\/layout');\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u89c6\u56fe\u6587\u4ef6index.php\u7684\u5e03\u5c40\u8bbe\u7f6e\u4e3acommon\/layout.html\u3002<\/p>\n<p>\u5728\u5e03\u5c40\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528yield\u8bed\u53e5\u6765\u5b9a\u4e49\u63d2\u69fd\uff0c\u7136\u540e\u5728\u5b50\u89c6\u56fe\u4e2d\u4f7f\u7528section\u8bed\u53e5\u6765\u586b\u5145\u5b83\u4eec\uff1a<\/p>\n<pre>\n\n\n    &lt;title&gt;My Application&lt;\/title&gt;&lt;header&gt;&lt;?php echo $this-&gt;section('header');?&amp;gt;\n    &lt;\/header&gt;&lt;main&gt;&lt;?php echo $this-&gt;section('main');?&amp;gt;\n    &lt;\/main&gt;&lt;footer&gt;&lt;?php echo $this-&gt;section('footer');?&amp;gt;\n    &lt;\/footer&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e09\u4e2a\u63d2\u69fd\uff0c\u5206\u522b\u5728header\u3001main\u548cfooter\u4e2d\u3002\u5728\u5b50\u89c6\u56fe\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528section\u8bed\u53e5\u6765\u586b\u5145\u5b83\u4eec\uff1a<\/p>\n<pre>&lt;?php echo $this-&gt;extend('common\/layout');?&amp;gt;\n&lt;?php echo $this-&gt;section('header');?&amp;gt;\n    &lt;h1&gt;Welcome to My Application&lt;\/h1&gt;\n&lt;?php echo $this-&gt;endSection();?&amp;gt;\n&lt;?php echo $this-&gt;section('main');?&amp;gt;\n    &lt;p&gt;This is the main content of my application.&lt;\/p&gt;\n&lt;?php echo $this-&gt;endSection();?&amp;gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528extend\u548csection\u6765\u6269\u5c55\u548c\u586b\u5145\u89c6\u56fe\u7684\u63d2\u69fd\u3002<\/p>\n<ol>\n<li>\u89c6\u56fe\u7684\u53d8\u91cf\u548c\u5757<\/li>\n<\/ol>\n<p>\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528assign\u65b9\u6cd5\u6765\u5206\u914d\u53d8\u91cf\u7ed9\u89c6\u56fe\uff1a<\/p>\n<pre>use thinkacadeView;\n\nclass Index\n{\n    public function index()\n    {\n        return View::fetch('index', [\n            'title' =&amp;gt; 'Welcome to My Application',\n            'content' =&amp;gt; 'This is the main content of my application.'\n        ]);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528assign\u65b9\u6cd5\u4e3a\u89c6\u56fe\u5206\u914d\u53d8\u91cf\u3002\u5728\u89c6\u56fe\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528echo\u6216=\u8bed\u53e5\u6765\u8f93\u51fa\u5b83\u4eec\uff1a<\/p>\n<pre>\n\n\n    &lt;title&gt;&lt;?php echo $title;?&gt;&lt;\/title&gt;&lt;p&gt;&lt;?php echo $content;?&gt;&lt;\/p&gt;\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u5728\u89c6\u56fe\u4e2d\u8f93\u51fa\u5206\u914d\u7684\u53d8\u91cf\u3002<\/p>\n<p>\u53e6\u5916\uff0c\u5728\u89c6\u56fe\u4e2d\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5757\u3002\u5757\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u8bed\u6cd5\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u7f16\u5199\u53ef\u91cd\u590d\u4f7f\u7528\u7684HTML\u7ed3\u6784\uff0c\u53ef\u4ee5\u7528\u4e8e\u6784\u5efa\u5bfc\u822a\u83dc\u5355\u3001\u6a21\u6001\u6846\u3001\u8868\u683c\u7b49\u7b49\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528block\u548cshow\u65b9\u6cd5\u6765\u5b9a\u4e49\u548c\u5c55\u793a\u5757\uff1a<\/p>\n<pre>\n\n\n    &lt;title&gt;My Application&lt;\/title&gt;&lt;?php echo $this-&gt;block('content');?&amp;gt;\n        &lt;p&gt;This is the main content of my application.&lt;\/p&gt;\n    &lt;?php echo $this-&gt;endBlock();?&amp;gt;\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3acontent\u7684\u5757\uff0c\u5e76\u5728\u5176\u4e2d\u5b9a\u4e49\u4e86\u4e00\u4e9b\u5185\u5bb9\u3002\u5728\u5b50\u89c6\u56fe\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528show\u65b9\u6cd5\u6765\u5c55\u793a\u5b83\uff1a<\/p>\n<pre>&lt;?php echo $this-&gt;extend('common\/layout');?&amp;gt;\n\n&lt;?php echo $this-&gt;section('main');?&amp;gt;\n    &lt;?php echo $this-&gt;show('content');?&amp;gt;\n&lt;?php echo $this-&gt;endSection();?&amp;gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u901a\u8fc7show\u65b9\u6cd5\u6765\u5c55\u793a\u5757\u3002<\/p>\n<ol>\n<li>\u603b\u7ed3<\/li>\n<\/ol>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528ThinkPHP6\u7684\u89c6\u56fe\u7ec4\u4ef6\u6765\u6784\u5efa\u9ad8\u8d28\u91cf\u7684\u7f51\u9875\u3002\u6211\u4eec\u4e86\u89e3\u4e86\u89c6\u56fe\u7684\u57fa\u672c\u6982\u5ff5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u89c6\u56fe\u7ec4\u4ef6\u6765\u6e32\u67d3\u89c6\u56fe\u3001\u5b9a\u4e49\u5e03\u5c40\u548c\u63d2\u69fd\u3001\u4f7f\u7528\u53d8\u91cf\u548c\u5757\u7b49\u3002\u901a\u8fc7\u5b66\u4e60\u8fd9\u4e9b\u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u9ad8\u81ea\u5df1\u7684\u5f00\u53d1\u6548\u7387\uff0c\u5e76\u6784\u5efa\u66f4\u52a0\u9ad8\u6548\u548c\u53cb\u597d\u7684\u5e94\u7528\u7a0b\u5e8f\u548c\u7f51\u7ad9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528ThinkPHP6\u7684\u89c6\u56fe\u7ec4\u4ef6\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\u5feb\u901f\u53d1\u5c55\uff0c\u7f51\u7ad9\u548c\u5e94\u7528\u7a0b\u5e8f\u53d8\u5f97\u8d8a\u6765\u8d8a\u590d\u6742\uff0c\u8fd9\u5c31\u9700\u8981\u4e00\u4e2a\u9ad8\u6548\u7684\u6846\u67b6\u6765\u7f29\u77ed\u5f00\u53d1\u5468\u671f\u3002\u662f\u4e00\u4e2a\u9886\u5148\u7684php\u6846\u67b6\uff0c\u63d0\u4f9b\u4e00\u7cfb\u5217\u5f3a\u5927\u7684\u529f\u80fd\u6765\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5feb\u901f\u6784\u5efa\u9ad8\u8d28\u91cf\u7684\u5e94\u7528\u7a0b\u5e8f\u3002 ThinkPHP\u76846\u7248\u672c\u5f15\u5165\u4e86\u4e00\u4e2a\u5168\u65b0\u7684\u89c6\u56fe\u7ec4\u4ef6\uff0c\u4f7f\u5f97\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u66f4\u52a0\u8f7b\u677e\u5730\u6784\u5efa\u52a8\u6001\u7684\u7f51\u9875\uff0c\u540c\u65f6\u4e5f\u80fd\u591f\u63d0\u9ad8\u5e94\u7528\u7a0b\u5e8f\u7684\u6027\u80fd\u548c\u6613\u7528\u6027\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528ThinkPHP6\u7684\u89c6\u56fe\u7ec4\u4ef6\u3002 \u6982\u8ff0 \u89c6\u56fe\u662fMVC\u67b6\u6784\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u662f\u6307\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8d1f\u8d23\u5728\u7f51\u9875\u4e2d\u663e\u793a\u6570\u636e\u7684\u90e8\u5206\u3002ThinkPHP6\u7684\u89c6\u56fe\u7ec4\u4ef6\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5c06\u9875\u9762\u548c\u4e1a\u52a1\u903b\u8f91\u4ee3\u7801\u5206\u79bb\uff0c\u4ee5\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002 \u89c6\u56fe\u7684\u4f7f\u7528 \u5728ThinkPHP6\u4e2d\uff0c\u89c6\u56fe\u6587\u4ef6\u5b58\u653e\u5728\/views\u76ee\u5f55\u4e0b\uff0c\u9ed8\u8ba4\u4e3a\/index.html\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528View\u7c7b\u6765\u6e32\u67d3\u89c6\u56fe\uff1a use thinkacadeView; class Index { public function index() { return View::fetch(&#8216;index&#8217;); } } \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u5728\u63a7\u5236\u5668\u4e2d\u4f7f\u7528View\u7c7b\u6765\u6e32\u67d3\u89c6\u56fe\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u89c6\u56fe\u7684\u7ee7\u627f\u548c\u5e03\u5c40 \u89c6\u56fe\u7684\u7ee7\u627f\u548c\u5e03\u5c40\u662f\u4e00\u79cd\u975e\u5e38\u5e38\u89c1\u7684\u6280\u672f\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u66f4\u6709\u6548\u5730\u7f16\u5199\u89c6\u56fe\u4ee3\u7801\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528layout\u65b9\u6cd5\u6765\u6307\u5b9a\u89c6\u56fe\u7684\u5e03\u5c40\uff1a use thinkacadeView; class Index { public function index() { return View::fetch(&#8216;index&#8217;)-&amp;gt;layout(&#8216;common\/layout&#8217;); } } \u767b\u5f55\u540e\u590d\u5236 \u4ee5\u4e0a\u4ee3\u7801\u5c06\u89c6\u56fe\u6587\u4ef6index.php\u7684\u5e03\u5c40\u8bbe\u7f6e\u4e3acommon\/layout.html\u3002 \u5728\u5e03\u5c40\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528yield\u8bed\u53e5\u6765\u5b9a\u4e49\u63d2\u69fd\uff0c\u7136\u540e\u5728\u5b50\u89c6\u56fe\u4e2d\u4f7f\u7528section\u8bed\u53e5\u6765\u586b\u5145\u5b83\u4eec\uff1a &lt;title&gt;My Application&lt;\/title&gt;&lt;header&gt;&lt;?php echo $this-&gt;section(&#8216;header&#8217;);?&amp;gt; &lt;\/header&gt;&lt;main&gt;&lt;?php echo $this-&gt;section(&#8216;main&#8217;);?&amp;gt; &lt;\/main&gt;&lt;footer&gt;&lt;?php echo $this-&gt;section(&#8216;footer&#8217;);?&amp;gt; &lt;\/footer&gt; \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e09\u4e2a\u63d2\u69fd\uff0c\u5206\u522b\u5728header\u3001main\u548cfooter\u4e2d\u3002\u5728\u5b50\u89c6\u56fe\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528section\u8bed\u53e5\u6765\u586b\u5145\u5b83\u4eec\uff1a &lt;?php echo [&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-23649","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23649","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=23649"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23649\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}