{"id":22445,"date":"2024-11-21T08:03:24","date_gmt":"2024-11-21T00:03:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22445\/"},"modified":"2024-11-21T08:03:24","modified_gmt":"2024-11-21T00:03:24","slug":"thinkphp5-0%e6%80%8e%e4%b9%88%e5%a2%9e%e5%88%a0%e6%9f%a5%e6%94%b9","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22445\/","title":{"rendered":"thinkphp5.0\u600e\u4e48\u589e\u5220\u67e5\u6539"},"content":{"rendered":"<p>\u4e00\u3001\u5f15\u8a00<\/p>\n<p>ThinkPHP\u662f\u4e00\u6b3e\u5f00\u6e90\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u81f4\u529b\u4e8e\u4e3a\u5f00\u53d1\u8005\u6784\u5efa\u9ad8\u6548\u3001\u7a33\u5b9a\u3001\u5b89\u5168\u4e14\u6613\u4e8e\u6269\u5c55\u7684WEB\u5e94\u7528\u63d0\u4f9b\u5e2e\u52a9\u3002thinkPHP \u6846\u67b6\u7248\u672c\u5347\u7ea7\u548c\u65b0\u589e\u529f\u80fd\u662f\u4e0d\u53ef\u907f\u514d\u7684\uff0c\u4e0b\u9762\u6211\u5c06\u901a\u8fc7\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528TP5.0\u5b9e\u73b0\u589e\u5220\u67e5\u6539\u529f\u80fd\uff0c\u5e0c\u671b\u80fd\u591f\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4e8c\u3001\u5b9e\u73b0\u589e\u5220\u67e5\u6539\u7684\u6b65\u9aa4<\/p>\n<ol>\n<li>\u51c6\u5907\u793a\u4f8b\u6570\u636e\u5e93\u548c\u8868<\/li>\n<\/ol>\n<p>\u9996\u5148\uff0c\u9700\u8981\u51c6\u5907\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u5e93\u548c\u8868\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u8868\uff1a<\/p>\n<p>CREATE TABLE think_user (<br \/> &nbsp;id int(11) NOT NULL AUTO_INCREMENT COMMENT &#8216;\u7528\u6237id&#8217;,<br \/> &nbsp;name varchar(255) DEFAULT NULL COMMENT &#8216;\u7528\u6237\u6635\u79f0&#8217;,<br \/> &nbsp;email varchar(255) DEFAULT NULL COMMENT &#8216;\u7528\u6237\u90ae\u7bb1&#8217;,<br \/> &nbsp;status tinyint(4) DEFAULT &#8216;1&#8217; COMMENT &#8216;\u7528\u6237\u72b6\u6001&#8217;,<br \/> &nbsp;create_time datetime DEFAULT NULL COMMENT &#8216;\u521b\u5efa\u65f6\u95f4&#8217;,<br \/> &nbsp;update_time datetime DEFAULT NULL COMMENT &#8216;\u66f4\u65b0\u65f6\u95f4&#8217;,<br \/> &nbsp;PRIMARY KEY (id)<br \/>) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=&#8217;\u7528\u6237\u4fe1\u606f\u8868&#8217;;<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ol>\n<li>\u521b\u5efa\u7528\u6237\u6a21\u578b\u548c\u63a7\u5236\u5668<\/li>\n<\/ol>\n<p>\u9996\u5148\u9700\u8981\u901a\u8fc7\u751f\u6210\u547d\u4ee4\u6765\u521b\u5efa\u7528\u6237\u6a21\u578b\u548c\u63a7\u5236\u5668\uff1a<\/p>\n<p>php think make:model User<br \/> php think make:controller User<\/p>\n<p>\u53ef\u53c2\u8003\u547d\u4ee4\u6587\u6863\uff1ahttps:\/\/www.kancloud.cn\/manual\/thinkphp5_1\/353947<\/p>\n<ol>\n<li>\u5b9e\u73b0\u7528\u6237\u5217\u8868<\/li>\n<\/ol>\n<p>\u5728\u7528\u6237\u63a7\u5236\u5668\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u5b9e\u73b0\u7528\u6237\u5217\u8868\u7684\u5c55\u793a\uff0c\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>public function index()<br \/> {<\/p>\n<pre> $users = UserModel::all();\n $this-&gt;assign('users', $users);\n return $this-&gt;fetch();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>}<\/p>\n<p>\u5176\u4e2d\uff0cUserModel\u4e3a\u5728\u7b2c2\u6b65\u4e2d\u521b\u5efa\u7684\u7528\u4e8e\u64cd\u4f5c\u7528\u6237\u8868\u7684\u6a21\u578b\uff0c$users\u8868\u793a\u4eceuser\u8868\u4e2d\u83b7\u53d6\u7684\u6240\u6709\u7528\u6237\u4fe1\u606f\uff0c$this-&gt;assign()\u7528\u4e8e\u5411\u6a21\u677f\u4e2d\u4f20\u9012\u53d8\u91cf\uff0c\u6700\u540e\u901a\u8fc7$this-&gt;fetch()\u65b9\u6cd5\u6e32\u67d3\u6a21\u677f\u3002<\/p>\n<ol>\n<li>\u5b9e\u73b0\u7528\u6237\u6dfb\u52a0<\/li>\n<\/ol>\n<p>\u7528\u6237\u6dfb\u52a0\u7684\u5b9e\u73b0\u76f8\u5bf9\u8f83\u7b80\u5355\uff0c\u5982\u4e0b\u4e00\u4e2a\u5b9e\u73b0\u7684\u53c2\u8003\u4ee3\u7801\uff1a<\/p>\n<p>public function add()<br \/> {<\/p>\n<pre> if (Request::instance()-&gt;isPost()) {\n     $user = new UserModel;\n     $user-&gt;name = input('name');\n     $user-&gt;email = input('email');\n     if ($user-&gt;save()) {\n         return $this-&gt;success('\u7528\u6237\u6dfb\u52a0\u6210\u529f\uff01', url('index'));\n     } else {\n         return $this-&gt;error('\u7528\u6237\u6dfb\u52a0\u5931\u8d25\uff01');\n     }\n }\n return $this-&gt;fetch();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>}<\/p>\n<ol>\n<li>\u5b9e\u73b0\u7528\u6237\u7f16\u8f91<\/li>\n<\/ol>\n<p>\u7528\u6237\u7f16\u8f91\u7684\u5b9e\u73b0\u9700\u8981\u66f4\u591a\u7684\u6ce8\u610f\uff0c\u5177\u4f53\u5b9e\u73b0\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<p>public function edit($id)<br \/> {<\/p>\n<pre> $user = UserModel::get($id);\n if (Request::instance()-&gt;isPost()) {\n     $user-&gt;name = input('name');\n     $user-&gt;email = input('email');\n     if ($user-&gt;save()) {\n         return $this-&gt;success('\u7528\u6237\u66f4\u65b0\u6210\u529f\uff01', url('index'));\n     } else {\n         return $this-&gt;error('\u7528\u6237\u66f4\u65b0\u5931\u8d25\uff01');\n     }\n }\n $this-&gt;assign('user', $user);\n return $this-&gt;fetch();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>}<\/p>\n<p>\u5176\u4e2d\uff0c$id\u4ee3\u8868\u7f16\u8f91\u7684\u7528\u6237Id\uff0cUserModel::get($id)\u7528\u4e8e\u83b7\u53d6\u5bf9\u5e94\u7684\u7528\u6237\u4fe1\u606f\uff0c$this-&gt;assign()\u7528\u4e8e\u4f20\u503c\u3002<\/p>\n<ol>\n<li>\u5b9e\u73b0\u7528\u6237\u5220\u9664<\/li>\n<\/ol>\n<p>\u7528\u6237\u5220\u9664\u9700\u8981\u8c28\u614e\u64cd\u4f5c\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u5220\u9664\u5b9e\u73b0\uff1a<\/p>\n<p>public function delete($id)<br \/> {<\/p>\n<pre> $user = UserModel::get($id);\n if ($user) {\n     $user-&gt;delete();\n     return $this-&gt;success('\u7528\u6237\u5220\u9664\u6210\u529f\uff01', url('index'));\n } else {\n     return $this-&gt;error('\u7528\u6237\u4e0d\u5b58\u5728\uff01');\n }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>}<\/p>\n<ol>\n<li>\u4ee3\u7801\u6d4b\u8bd5<\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5df2\u7ecf\u5b9e\u73b0\u4e86\u7528\u6237\u7ba1\u7406\u7684\u57fa\u672c\u529f\u80fd\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u4ee5\u4e0bURL\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<p>\u8bbf\u95ee \/user\/index \u53ef\u4ee5\u67e5\u770b\u5230\u7528\u6237\u5217\u8868<br \/>\u8bbf\u95ee \/user\/add \u53ef\u4ee5\u6dfb\u52a0\u65b0\u7684\u7528\u6237<br \/>\u8bbf\u95ee \/user\/edit\/id \u7f16\u8f91\u6307\u5b9aID\u7684\u7528\u6237<br \/>\u8bbf\u95ee \/user\/delete\/id \u5220\u9664\u6307\u5b9aID\u7684\u7528\u6237<\/p>\n<p>\u4e09\u3001\u603b\u7ed3<\/p>\n<p>\u5728TP5.0\u4e2d\u5b9e\u73b0\u589e\u5220\u67e5\u6539\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u52a0\u5feb\u5f00\u53d1\u4eba\u5458\u7684\u5f00\u53d1\u6548\u7387\u3002\u901a\u8fc7\u4ee5\u4e0a\u7684\u5b9e\u73b0\u6b65\u9aa4\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u4eba\u5458\u5b9e\u73b0\u7b80\u5355\u7684\u589e\u5220\u67e5\u6539\u529f\u80fd\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u53c2\u8003TP5.0\u5b98\u65b9\u6587\u6863\u6765\u66f4\u6df1\u5165\u5730\u4e86\u89e3TP5.0\u6846\u67b6\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp5.0\u600e\u4e48\u589e\u5220\u67e5\u6539\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\u5f15\u8a00 ThinkPHP\u662f\u4e00\u6b3e\u5f00\u6e90\u7684PHP\u5f00\u53d1\u6846\u67b6\uff0c\u81f4\u529b\u4e8e\u4e3a\u5f00\u53d1\u8005\u6784\u5efa\u9ad8\u6548\u3001\u7a33\u5b9a\u3001\u5b89\u5168\u4e14\u6613\u4e8e\u6269\u5c55\u7684WEB\u5e94\u7528\u63d0\u4f9b\u5e2e\u52a9\u3002thinkPHP \u6846\u67b6\u7248\u672c\u5347\u7ea7\u548c\u65b0\u589e\u529f\u80fd\u662f\u4e0d\u53ef\u907f\u514d\u7684\uff0c\u4e0b\u9762\u6211\u5c06\u901a\u8fc7\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528TP5.0\u5b9e\u73b0\u589e\u5220\u67e5\u6539\u529f\u80fd\uff0c\u5e0c\u671b\u80fd\u591f\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002 \u4e8c\u3001\u5b9e\u73b0\u589e\u5220\u67e5\u6539\u7684\u6b65\u9aa4 \u51c6\u5907\u793a\u4f8b\u6570\u636e\u5e93\u548c\u8868 \u9996\u5148\uff0c\u9700\u8981\u51c6\u5907\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u5e93\u548c\u8868\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u8868\uff1a CREATE TABLE think_user ( &nbsp;id int(11) NOT NULL AUTO_INCREMENT COMMENT &#8216;\u7528\u6237id&#8217;, &nbsp;name varchar(255) DEFAULT NULL COMMENT &#8216;\u7528\u6237\u6635\u79f0&#8217;, &nbsp;email varchar(255) DEFAULT NULL COMMENT &#8216;\u7528\u6237\u90ae\u7bb1&#8217;, &nbsp;status tinyint(4) DEFAULT &#8216;1&#8217; COMMENT &#8216;\u7528\u6237\u72b6\u6001&#8217;, &nbsp;create_time datetime DEFAULT NULL COMMENT &#8216;\u521b\u5efa\u65f6\u95f4&#8217;, &nbsp;update_time datetime DEFAULT NULL COMMENT &#8216;\u66f4\u65b0\u65f6\u95f4&#8217;, &nbsp;PRIMARY KEY (id)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT=&#8217;\u7528\u6237\u4fe1\u606f\u8868&#8217;; \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b [&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-22445","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22445","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=22445"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22445\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}