{"id":23295,"date":"2024-11-21T09:51:01","date_gmt":"2024-11-21T01:51:01","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23295\/"},"modified":"2024-11-21T09:51:01","modified_gmt":"2024-11-21T01:51:01","slug":"thinkphp6%e7%9a%84%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98%e8%a7%a3%e5%86%b3%e5%8a%9e%e6%b3%95%e6%9c%89%e5%93%aa%e4%ba%9b%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23295\/","title":{"rendered":"ThinkPHP6\u7684\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u529e\u6cd5\u6709\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p>6\u662f\u4e00\u6b3e\u5e7f\u53d7\u6b22\u8fce\u7684php\u6846\u67b6\uff0c\u867d\u7136\u5176\u62e5\u6709\u8bb8\u591a\u4f18\u52bf\u548c\u4fbf\u6377\u6027\uff0c\u4f46\u662f\u6211\u4eec\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u4ecd\u7136\u4f1a\u906d\u9047\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u5982\u679c\u4e0d\u53ca\u65f6\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c\u5c31\u6709\u53ef\u80fd\u963b\u788d\u9879\u76ee\u7684\u6b63\u5e38\u8fdb\u884c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5\u3002<\/p>\n<p>1.\u6570\u636e\u5e93\u8fde\u63a5\u95ee\u9898<br \/>\u5728\u4f7f\u7528ThinkPHP6\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u906d\u9047\u65e0\u6cd5\u8fde\u63a5\u6570\u636e\u5e93\u7684\u95ee\u9898\u3002\u8fd9\u65f6\u5019\uff0c\u6211\u4eec\u5e94\u8be5\u9996\u5148\u68c0\u67e5\u6570\u636e\u5e93\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u5728config\/database.php\u4e2d\uff0c\u5e94\u8be5\u914d\u7f6e\u6b63\u786e\u7684\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>return [\n    'type'        =&amp;gt; 'mysql',\n    'hostname'    =&amp;gt; 'localhost',\n    'database'    =&amp;gt; 'test',\n    'username'    =&amp;gt; 'root',\n    'password'    =&amp;gt; '',\n    'hostport'    =&amp;gt; '',\n    'charset'     =&amp;gt; 'utf8mb4',\n    'prefix'      =&amp;gt; '',\n    'debug'       =&amp;gt; true,\n    'deploy'      =&amp;gt; 0,\n    'rw_separate' =&amp;gt; false,\n    'master_num'  =&amp;gt; 1,\n    'slave_no'    =&amp;gt; '',\n    'fields_strict' =&amp;gt; true,\n    'resultset_type' =&amp;gt; 'array',\n    'auto_timestamp' =&amp;gt; false,\n    'datetime_format' =&amp;gt; 'Y-m-d H:i:s',\n    'sql_explain' =&amp;gt; false,\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>2.\u65e0\u6cd5\u52a0\u8f7d\u6a21\u677f\u6587\u4ef6<br \/>\u5728\u4f7f\u7528ThinkPHP6\u7684\u6a21\u677f\u529f\u80fd\u65f6\uff0c\u6709\u65f6\u5019\u4f1a\u906d\u9047\u65e0\u6cd5\u52a0\u8f7d\u6a21\u677f\u6587\u4ef6\u7684\u95ee\u9898\u3002\u8fd9\u65f6\u5019\uff0c\u6211\u4eec\u5e94\u8be5\u68c0\u67e5\u6a21\u677f\u6587\u4ef6\u8def\u5f84\u662f\u5426\u6b63\u786e\u3002\u6211\u4eec\u53ef\u4ee5\u5728config\/view.php\u4e2d\u914d\u7f6e\u6a21\u677f\u6587\u4ef6\u7684\u8def\u5f84\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>return [\n    \/\/ \u6a21\u677f\u8def\u5f84\n    'view_path'    =&amp;gt; '.\/template\/',\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u786e\u5b9a\u6a21\u677f\u6587\u4ef6\u7684\u540e\u7f00\u540d\u662f\u5426\u6b63\u786e\u3002\u5728config\/view.php\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u914d\u7f6e\u6a21\u677f\u6587\u4ef6\u7684\u540e\u7f00\u540d\uff1a<\/p>\n<pre>return [\n    'view_suffix'  =&amp;gt; 'html',\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6216\u8005\uff0c\u5728Controller\u4e2d\u7684\u65b9\u6cd5\u4e2d\u8c03\u7528\u6a21\u677f\u65f6\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6765\u6307\u5b9a\u6a21\u677f\u6587\u4ef6\u7684\u540e\u7f00\u540d\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>return $this-&amp;gt;fetch('index', 'html');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>3.\u8def\u7531\u95ee\u9898<br \/>\u5728\u4f7f\u7528ThinkPHP6\u7684\u8def\u7531\u673a\u5236\u65f6\uff0c\u6709\u65f6\u5019\u4f1a\u906d\u9047\u8def\u7531\u65e0\u6cd5\u6b63\u5e38\u5de5\u4f5c\u7684\u95ee\u9898\u3002\u8fd9\u65f6\u5019\uff0c\u6211\u4eec\u5e94\u8be5\u5148\u68c0\u67e5\u8def\u7531\u662f\u5426\u6b63\u786e\u914d\u7f6e\u3002\u5728config\/route.php\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u914d\u7f6e\u8def\u7531\u89c4\u5219\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>use thinkacadeRoute;\n\nRoute::get('\/user\/:id', 'index\/User\/read');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c\/user\/:id\u8868\u793a\u4e00\u4e2a\u8def\u7531\u89c4\u5219\uff0c:id\u8868\u793a\u4e00\u4e2a\u5360\u4f4d\u7b26\uff0c\u8868\u793aURL\u4e2d\u7684\u4e00\u4e2a\u53d8\u91cf\u3002\u6211\u4eec\u9700\u8981\u786e\u4fdd\u8def\u7531\u89c4\u5219\u6b63\u786e\u914d\u7f6e\uff0c\u4e14\u5360\u4f4d\u7b26\u540d\u79f0\u4e0e\u5bf9\u5e94\u53c2\u6570\u540d\u79f0\u4e00\u81f4\u3002<\/p>\n<p>4.\u6587\u4ef6\u4e0a\u4f20\u95ee\u9898<br \/>\u5728\u4f7f\u7528ThinkPHP6\u8fdb\u884c\u6587\u4ef6\u4e0a\u4f20\u65f6\uff0c\u6709\u65f6\u5019\u4f1a\u906d\u9047\u65e0\u6cd5\u6b63\u5e38\u4e0a\u4f20\u6587\u4ef6\u7684\u95ee\u9898\u3002\u8fd9\u65f6\u5019\uff0c\u6211\u4eec\u5e94\u8be5\u68c0\u67e5\u4e0a\u4f20\u6587\u4ef6\u5927\u5c0f\u662f\u5426\u8d85\u51fa\u9650\u5236\u3002\u5728config\/upload.php\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8bbe\u7f6e\u4e0a\u4f20\u6587\u4ef6\u5927\u5c0f\u7684\u9650\u5236\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>return [\n    'default' =&amp;gt; [\n        'size' =&amp;gt; 1024 * 1024 * 2, \/\/\u6587\u4ef6\u5927\u5c0f\u9650\u5236\u4e3a2M\n    ],\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u786e\u4fdd\u4e0a\u4f20\u6587\u4ef6\u7684\u76ee\u5f55\u5b58\u5728\u4e14\u53ef\u5199\u3002\u6211\u4eec\u53ef\u4ee5\u5728config\/filesystem.php\u4e2d\u914d\u7f6e\u6587\u4ef6\u4e0a\u4f20\u76ee\u5f55\u7684\u8def\u5f84\uff1a<\/p>\n<pre>return [\n    'default' =&amp;gt; 'local',\n    'disks'   =&amp;gt; [\n        'local' =&amp;gt; [\n            'type' =&amp;gt; 'local',\n            'root' =&amp;gt; app()-&amp;gt;getRuntimePath() . 'upload',\n        ],\n    ],\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u76ee\u5f55\u4e0d\u5b58\u5728\uff0c\u5219\u9700\u8981\u624b\u52a8\u521b\u5efa\u5e76\u8bbe\u7f6e\u6b63\u786e\u6743\u9650\u3002<\/p>\n<p>\u603b\u4e4b\uff0c\u4f7f\u7528ThinkPHP6\u6846\u67b6\u65f6\uff0c\u906d\u9047\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u662f\u5e38\u6709\u7684\u4e8b\u60c5\u3002\u6211\u4eec\u9700\u8981\u53ca\u65f6\u67e5\u627e\u95ee\u9898\u6839\u6e90\uff0c\u7136\u540e\u5c1d\u8bd5\u7740\u89e3\u51b3\u95ee\u9898\u3002\u672c\u6587\u53ea\u4ecb\u7ecd\u4e86\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5\uff0c\u95ee\u9898\u7684\u591a\u6837\u6027\u4e0e\u590d\u6742\u6027\u662f\u6211\u4eec\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u5e94\u8be5\u6df1\u523b\u8ba4\u8bc6\u5230\u7684\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP6\u7684\u5e38\u89c1\u95ee\u9898\u89e3\u51b3\u529e\u6cd5\u6709\u54ea\u4e9b\uff1f\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>6\u662f\u4e00\u6b3e\u5e7f\u53d7\u6b22\u8fce\u7684php\u6846\u67b6\uff0c\u867d\u7136\u5176\u62e5\u6709\u8bb8\u591a\u4f18\u52bf\u548c\u4fbf\u6377\u6027\uff0c\u4f46\u662f\u6211\u4eec\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u4ecd\u7136\u4f1a\u906d\u9047\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u5982\u679c\u4e0d\u53ca\u65f6\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\uff0c\u5c31\u6709\u53ef\u80fd\u963b\u788d\u9879\u76ee\u7684\u6b63\u5e38\u8fdb\u884c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u89c1\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5\u3002 1.\u6570\u636e\u5e93\u8fde\u63a5\u95ee\u9898\u5728\u4f7f\u7528ThinkPHP6\u8fdb\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u906d\u9047\u65e0\u6cd5\u8fde\u63a5\u6570\u636e\u5e93\u7684\u95ee\u9898\u3002\u8fd9\u65f6\u5019\uff0c\u6211\u4eec\u5e94\u8be5\u9996\u5148\u68c0\u67e5\u6570\u636e\u5e93\u7684\u914d\u7f6e\u6587\u4ef6\u3002\u5728config\/database.php\u4e2d\uff0c\u5e94\u8be5\u914d\u7f6e\u6b63\u786e\u7684\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f\u3002\u4f8b\u5982\uff1a return [ &#8216;type&#8217; =&amp;gt; &#8216;mysql&#8217;, &#8216;hostname&#8217; =&amp;gt; &#8216;localhost&#8217;, &#8216;database&#8217; =&amp;gt; &#8216;test&#8217;, &#8216;username&#8217; =&amp;gt; &#8216;root&#8217;, &#8216;password&#8217; =&amp;gt; &#8221;, &#8216;hostport&#8217; =&amp;gt; &#8221;, &#8216;charset&#8217; =&amp;gt; &#8216;utf8mb4&#8217;, &#8216;prefix&#8217; =&amp;gt; &#8221;, &#8216;debug&#8217; =&amp;gt; true, &#8216;deploy&#8217; =&amp;gt; 0, &#8216;rw_separate&#8217; =&amp;gt; false, &#8216;master_num&#8217; =&amp;gt; 1, &#8216;slave_no&#8217; =&amp;gt; &#8221;, &#8216;fields_strict&#8217; =&amp;gt; true, &#8216;resultset_type&#8217; =&amp;gt; &#8216;array&#8217;, &#8216;auto_timestamp&#8217; =&amp;gt; false, &#8216;datetime_format&#8217; =&amp;gt; &#8216;Y-m-d [&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-23295","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23295","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=23295"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23295\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}