{"id":23646,"date":"2024-11-21T16:40:03","date_gmt":"2024-11-21T08:40:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23646\/"},"modified":"2024-11-21T16:40:03","modified_gmt":"2024-11-21T08:40:03","slug":"%e5%9c%a8thinkphp6%e4%b8%ad%e4%bd%bf%e7%94%a8mvc%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23646\/","title":{"rendered":"\u5728ThinkPHP6\u4e2d\u4f7f\u7528MVC\u6a21\u5f0f"},"content":{"rendered":"<p>6\u662f\u4e00\u4e2a\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5b83\u4f7f\u7528mvc\uff08model-view-controller\uff09\u6a21\u5f0f\u6765\u7ec4\u7ec7\u5e94\u7528\u7a0b\u5e8f\u7684\u903b\u8f91\u7ed3\u6784\u3002mvc\u6a21\u5f0f\u662f\u4e00\u79cd\u7528\u4e8e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u5c06\u5e94\u7528\u7a0b\u5e8f\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a\u6a21\u578b\uff08model\uff09\u3001\u89c6\u56fe\uff08view\uff09\u548c\u63a7\u5236\u5668\uff08controller\uff09\u3002\u6bcf\u4e2a\u90e8\u5206\u90fd\u6709\u5176\u72ec\u7acb\u7684\u804c\u8d23\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728thinkphp6\u4e2d\u4f7f\u7528mvc\u6a21\u5f0f\u3002<\/p>\n<ol>\n<li>Model<\/li>\n<\/ol>\n<p>\u6a21\u578b\u662f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5904\u7406\u6570\u636e\u7684\u90e8\u5206\u3002\u5b83\u4eec\u901a\u5e38\u662f\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u4e2d\u95f4\u5c42\uff0c\u7528\u4e8e\u5728\u5e94\u7528\u7a0b\u5e8f\u548c\u6570\u636e\u5e93\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6a21\u578b\u901a\u5e38\u5b58\u50a8\u5728app \/ Model\u76ee\u5f55\u4e0b\u3002\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627fThinkPHP6\u4e2d\u7684Model\u7c7b\u6765\u521b\u5efa\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u6a21\u578b\uff1a<\/p>\n<pre>&lt;?php namespace appmodel;\n\nuse thinkModel;\n\nclass User extends Model\n{\n    protected $pk = 'user_id'; \/\/\u4e3b\u952e\u540d\n\n    public function getUserById($id)\n    {\n        return $this-&gt;where('user_id', $id)-&amp;gt;find();\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aUser\u6a21\u578b\uff0c\u5e76\u901a\u8fc7\u7ee7\u627fThinkPHP6\u4e2d\u7684Model\u7c7b\u6269\u5c55\u4e86\u5176\u529f\u80fd\u3002\u6211\u4eec\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e2agetUserById\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4ece\u6570\u636e\u5e93\u4e2d\u6839\u636e\u7528\u6237ID\u67e5\u627e\u7528\u6237\u6570\u636e\u5e76\u8fd4\u56de\u7ed3\u679c\u3002\u5728\u6a21\u578b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u5176\u4ed6\u4e0e\u6570\u636e\u4ea4\u4e92\u7684\u65b9\u6cd5\u4ee5\u53ca\u4e00\u4e9b\u9644\u52a0\u529f\u80fd\u3002<\/p>\n<ol>\n<li>View<\/li>\n<\/ol>\n<p>\u89c6\u56fe\u662f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u90e8\u5206\u3002\u5b83\u4eec\u901a\u5e38\u662fHTML\u754c\u9762\uff0c\u663e\u793a\u6570\u636e\u548c\u63a5\u6536\u7528\u6237\u8f93\u5165\u3002\u5728ThinkPHP6\u4e2d\uff0c\u89c6\u56fe\u901a\u5e38\u5b58\u50a8\u5728app \/ view\u76ee\u5f55\u4e0b\uff0c\u5e76\u4f7f\u7528PHP\u6587\u4ef6\u6a21\u677f\u6765\u7ec4\u7ec7\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u89c6\u56fe\uff1a<\/p>\n<pre>\n\n\n    &lt;title&gt;User Profile&lt;\/title&gt;&lt;h1&gt;= $user-&amp;gt;username ?&amp;gt;'s Profile&lt;\/h1&gt;\n    &lt;p&gt;Name: = $user-&amp;gt;name ?&amp;gt;&lt;\/p&gt;&lt;p&gt;&lt;span&gt;\u7acb\u5373\u5b66\u4e60&lt;\/span&gt;\u201c&lt;a href=\"https:\/\/pan.quark.cn\/s\/7fc7563c4182\" style=\"text-decoration: underline !important; color: blue; font-weight: bolder;\" rel=\"nofollow\" target=\"_blank\"&gt;PHP\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09&lt;\/a&gt;\u201d\uff1b&lt;\/p&gt;\n    &lt;p&gt;Email: = $user-&amp;gt;email ?&amp;gt;&lt;\/p&gt;\n\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aUser\u89c6\u56fe\uff0c\u5e76\u4f7f\u7528PHP\u4e2d\u7684= ?&gt;\u6807\u8bb0\u6765\u8f93\u51fa\u6765\u81ea\u63a7\u5236\u5668\u7684\u6570\u636e\u3002\u89c6\u56fe\u901a\u5e38\u4e0e\u63a7\u5236\u5668\u7d27\u5bc6\u8026\u5408\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u7528\u6237\u754c\u9762\u7684\u4e00\u90e8\u5206\u3002\u56e0\u6b64\uff0c\u5728\u5f00\u53d1\u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u89c6\u56fe\u80fd\u591f\u4f7f\u7528\u63a7\u5236\u5668\u4e2d\u7684\u6570\u636e\u6765\u64cd\u4f5c\u7528\u6237\u754c\u9762\u3002<\/p>\n<ol>\n<li>Controller<\/li>\n<\/ol>\n<p>\u63a7\u5236\u5668\u662f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5904\u7406\u7528\u6237\u8f93\u5165\u548c\u64cd\u4f5c\u6a21\u578b\u7684\u90e8\u5206\u3002\u5b83\u4eec\u901a\u5e38\u662f\u901a\u8fc7\u7528\u6237\u8bf7\u6c42\uff08\u5982URL\uff09\u8c03\u7528\u7684\u3002\u5728ThinkPHP6\u4e2d\uff0c\u63a7\u5236\u5668\u901a\u5e38\u5b58\u50a8\u5728app \/ controller\u76ee\u5f55\u4e0b\uff0c\u5e76\u901a\u8fc7\u5728\u8def\u7531\uff08route\uff09\u914d\u7f6e\u6587\u4ef6\u4e2d\u5b9a\u4e49\u6765\u5904\u7406\u8bf7\u6c42\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u63a7\u5236\u5668\uff1a<\/p>\n<pre>acadeView;\nuse appmodelUser;\n\nclass UserController\n{\n    public function profile($id)\n    {\n        $user = User::where('user_id', $id)-&amp;gt;find();\n        View::assign('user', $user);\n        return View::fetch('user\/profile');\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aUserController\u63a7\u5236\u5668\uff0c\u5e76\u4e14\u6709\u4e00\u4e2aprofile\u65b9\u6cd5\uff0c\u5f53\u7528\u6237\u8bbf\u95ee\/profile \/ id\u8def\u7531\u65f6\uff0c\u8be5\u65b9\u6cd5\u5c06\u88ab\u8c03\u7528\u3002\u8be5\u65b9\u6cd5\u4f7f\u7528User\u6a21\u578b\u4ece\u6570\u636e\u5e93\u4e2d\u67e5\u627e\u7528\u6237\u6570\u636e\uff0c\u5c06\u5176\u5206\u914d\u4e3a\u7528\u6237\u53d8\u91cf\uff0c\u7136\u540e\u5c06\u89c6\u56fe\u52a0\u8f7d\u4e3aPHP\u6587\u4ef6\u6a21\u677f\uff08user \/ profile\uff09\u3002\u8fd9\u4e2a\u6a21\u677f\u5c06\u663e\u793a\u7528\u6237\u8d44\u6599\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0cMVC\u6a21\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7ec4\u7ec7\u5e94\u7528\u7a0b\u5e8f\u7ed3\u6784\u7684\u65b9\u5f0f\u3002\u4f7f\u7528ThinkPHP6\u6846\u67b6\u4e2d\u7684MVC\u6a21\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u5206\u79bb\u5e94\u7528\u7a0b\u5e8f\u7684\u7279\u5b9a\u4efb\u52a1\u3002\u4f7f\u7528MVC\u7684\u4f18\u70b9\u4e4b\u4e00\u662f\u53ef\u4ee5\u4f7f\u5e94\u7528\u7a0b\u5e8f\u6613\u4e8e\u6269\u5c55\u548c\u7ef4\u62a4\uff0c\u56e0\u4e3a\u6bcf\u4e2a\u90e8\u5206\u90fd\u53ef\u4ee5\u72ec\u7acb\u5730\u8c03\u6574\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728ThinkPHP6\u4e2d\u4f7f\u7528MVC\u6a21\u5f0f\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\u4e2a\u6d41\u884c\u7684php\u6846\u67b6\uff0c\u5b83\u4f7f\u7528mvc\uff08model-view-controller\uff09\u6a21\u5f0f\u6765\u7ec4\u7ec7\u5e94\u7528\u7a0b\u5e8f\u7684\u903b\u8f91\u7ed3\u6784\u3002mvc\u6a21\u5f0f\u662f\u4e00\u79cd\u7528\u4e8e\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u7684\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u5b83\u5c06\u5e94\u7528\u7a0b\u5e8f\u5206\u4e3a\u4e09\u4e2a\u90e8\u5206\uff1a\u6a21\u578b\uff08model\uff09\u3001\u89c6\u56fe\uff08view\uff09\u548c\u63a7\u5236\u5668\uff08controller\uff09\u3002\u6bcf\u4e2a\u90e8\u5206\u90fd\u6709\u5176\u72ec\u7acb\u7684\u804c\u8d23\uff0c\u4f7f\u5f97\u5e94\u7528\u7a0b\u5e8f\u6613\u4e8e\u7ef4\u62a4\u548c\u6269\u5c55\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5728thinkphp6\u4e2d\u4f7f\u7528mvc\u6a21\u5f0f\u3002 Model \u6a21\u578b\u662f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5904\u7406\u6570\u636e\u7684\u90e8\u5206\u3002\u5b83\u4eec\u901a\u5e38\u662f\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92\u7684\u4e2d\u95f4\u5c42\uff0c\u7528\u4e8e\u5728\u5e94\u7528\u7a0b\u5e8f\u548c\u6570\u636e\u5e93\u4e4b\u95f4\u4f20\u9012\u6570\u636e\u3002\u5728ThinkPHP6\u4e2d\uff0c\u6a21\u578b\u901a\u5e38\u5b58\u50a8\u5728app \/ Model\u76ee\u5f55\u4e0b\u3002\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627fThinkPHP6\u4e2d\u7684Model\u7c7b\u6765\u521b\u5efa\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u6a21\u578b\uff1a &lt;?php namespace appmodel; use thinkModel; class User extends Model { protected $pk = &#8216;user_id&#8217;; \/\/\u4e3b\u952e\u540d public function getUserById($id) { return $this-&gt;where(&#8216;user_id&#8217;, $id)-&amp;gt;find(); } } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aUser\u6a21\u578b\uff0c\u5e76\u901a\u8fc7\u7ee7\u627fThinkPHP6\u4e2d\u7684Model\u7c7b\u6269\u5c55\u4e86\u5176\u529f\u80fd\u3002\u6211\u4eec\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e2agetUserById\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u4ece\u6570\u636e\u5e93\u4e2d\u6839\u636e\u7528\u6237ID\u67e5\u627e\u7528\u6237\u6570\u636e\u5e76\u8fd4\u56de\u7ed3\u679c\u3002\u5728\u6a21\u578b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u5176\u4ed6\u4e0e\u6570\u636e\u4ea4\u4e92\u7684\u65b9\u6cd5\u4ee5\u53ca\u4e00\u4e9b\u9644\u52a0\u529f\u80fd\u3002 View \u89c6\u56fe\u662f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4e0e\u7528\u6237\u4ea4\u4e92\u7684\u90e8\u5206\u3002\u5b83\u4eec\u901a\u5e38\u662fHTML\u754c\u9762\uff0c\u663e\u793a\u6570\u636e\u548c\u63a5\u6536\u7528\u6237\u8f93\u5165\u3002\u5728ThinkPHP6\u4e2d\uff0c\u89c6\u56fe\u901a\u5e38\u5b58\u50a8\u5728app \/ view\u76ee\u5f55\u4e0b\uff0c\u5e76\u4f7f\u7528PHP\u6587\u4ef6\u6a21\u677f\u6765\u7ec4\u7ec7\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u89c6\u56fe\uff1a &lt;title&gt;User Profile&lt;\/title&gt;&lt;h1&gt;= $user-&amp;gt;username ?&amp;gt;&#8217;s Profile&lt;\/h1&gt; &lt;p&gt;Name: = $user-&amp;gt;name ?&amp;gt;&lt;\/p&gt;&lt;p&gt;&lt;span&gt;\u7acb\u5373\u5b66\u4e60&lt;\/span&gt;\u201c&lt;a href=&#8221;https:\/\/pan.quark.cn\/s\/7fc7563c4182&#8243; style=&#8221;text-decoration: underline !important; color: blue; font-weight: bolder;&#8221; rel=&#8221;nofollow&#8221; target=&#8221;_blank&#8221;&gt;PHP\u514d\u8d39\u5b66\u4e60\u7b14\u8bb0\uff08\u6df1\u5165\uff09&lt;\/a&gt;\u201d\uff1b&lt;\/p&gt; &lt;p&gt;Email: [&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-23646","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23646","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=23646"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23646\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}