{"id":22700,"date":"2024-11-21T08:33:02","date_gmt":"2024-11-21T00:33:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/22700\/"},"modified":"2024-11-21T08:33:02","modified_gmt":"2024-11-21T00:33:02","slug":"thinkphp%e5%89%8d%e5%90%8e%e7%ab%af%e5%88%86%e7%a6%bb%e6%80%8e%e4%b9%88%e9%85%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/22700\/","title":{"rendered":"thinkphp\u524d\u540e\u7aef\u5206\u79bb\u600e\u4e48\u914d\u7f6e"},"content":{"rendered":"<p>\u968f\u7740\u524d\u540e\u7aef\u5206\u79bb\u6280\u672f\u7684\u666e\u53ca\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5f00\u53d1\u8005\u5f00\u59cb\u5c1d\u8bd5\u5c06\u81ea\u5df1\u7684\u9879\u76ee\u4ece\u4f20\u7edf\u7684mvc\u67b6\u6784\u8f6c\u6362\u4e3a\u524d\u540e\u7aef\u5206\u79bb\u7684\u67b6\u6784\u3002\u800cthinkphp\u4f5c\u4e3a\u4e00\u6b3e\u5f00\u6e90\u7684php\u6846\u67b6\uff0c\u4e5f\u79ef\u6781\u54cd\u5e94\u8fd9\u4e00\u6280\u672f\u6f6e\u6d41\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u914d\u7f6ethinkphp\u7684\u524d\u540e\u7aef\u5206\u79bb\u6a21\u5f0f\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u9879\u76ee\u4e2d\u5e94\u7528\u8be5\u6280\u672f\u3002<\/p>\n<ol>\n<li>\u73af\u5883\u914d\u7f6e<\/li>\n<\/ol>\n<p>\u5728\u4f7f\u7528thinkphp\u5b9e\u73b0\u524d\u540e\u7aef\u5206\u79bb\u4e4b\u524d\uff0c\u9700\u8981\u5148\u8fdb\u884c\u73af\u5883\u914d\u7f6e\u3002\u9996\u5148\uff0c\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\uff0c\u4f7f\u7528composer\u5b89\u88c5thinkphp\u6846\u67b6\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p>composer create-project topthink\/think<\/p>\n<p>\u7136\u540e\uff0c\u5728Apache\u6216Nginx\u4e2d\u914d\u7f6e\u865a\u62df\u4e3b\u673a\uff0c\u5c06\u9879\u76ee\u6839\u76ee\u5f55\u8bbe\u7f6e\u4e3a\u7f51\u7ad9\u6839\u76ee\u5f55\u3002\u540c\u65f6\uff0c\u8fd8\u9700\u8981\u5728PHP\u914d\u7f6e\u6587\u4ef6\u4e2d\u542f\u7528rewrite\u6a21\u5757\uff0c\u4f7f\u5f97thinkphp\u80fd\u591f\u6b63\u5e38\u4f7f\u7528\u8def\u7531\u529f\u80fd\u3002<\/p>\n<ol>\n<li>\u914d\u7f6e\u8def\u7531<\/li>\n<\/ol>\n<p>\u5728thinkphp\u4e2d\uff0c\u8def\u7531\u914d\u7f6e\u662f\u5173\u952e\u6b65\u9aa4\u4e4b\u4e00\uff0c\u4e5f\u662f\u524d\u540e\u7aef\u5206\u79bb\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u627e\u5230config\u76ee\u5f55\u4e2d\u7684route.php\u6587\u4ef6\uff0c\u5c06\u5176\u6253\u5f00\u3002\u7136\u540e\uff0c\u6839\u636e\u524d\u540e\u7aef\u5206\u79bb\u7684\u9700\u6c42\uff0c\u8fdb\u884c\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\uff081\uff09\u5c06\u9ed8\u8ba4\u7684\u8def\u7531\u914d\u7f6e\u6539\u4e3a\uff1a<\/p>\n<p>&#8216;\/&#8217; =&gt; &#8216;index\/index\/index&#8217;<\/p>\n<p>\u8fd9\u4e00\u6539\u52a8\u53ef\u4ee5\u4f7f\u5f97\u9ed8\u8ba4\u8bbf\u95ee\u7684\u9875\u9762\u53d8\u4e3a\u524d\u7aef\u9875\u9762\uff0c\u800c\u975e\u540e\u7aef\u9875\u9762\u3002<\/p>\n<p>\uff082\uff09\u5728\u8def\u7531\u914d\u7f6e\u4e2d\u6dfb\u52a0\u524d\u7aef\u9875\u9762\u7684\u8def\u7531\u89c4\u5219\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8216;@^\/:path$&#8217; =&gt; &#8216;index\/index\/index&#8217;<\/p>\n<p>\u5176\u4e2d\uff0c:path\u8868\u793a\u524d\u7aef\u9875\u9762\u7684\u8def\u5f84\u3002\u901a\u8fc7\u8fd9\u4e00\u89c4\u5219\uff0c\u53ef\u4ee5\u5b9e\u73b0\u524d\u7aef\u8def\u7531\u7684\u8df3\u8f6c\u3002<\/p>\n<p>\uff083\uff09\u5728\u8def\u7531\u914d\u7f6e\u4e2d\u6dfb\u52a0\u63a5\u53e3\u7684\u8def\u7531\u89c4\u5219\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8216;@^api\/:controller\/:action$&#8217; =&gt; &#8216;api\/:controller\/:action&#8217;<\/p>\n<p>\u5176\u4e2d\uff0c:controller\u8868\u793a\u63a7\u5236\u5668\u540d\uff0c:action\u8868\u793a\u65b9\u6cd5\u540d\u3002\u901a\u8fc7\u8fd9\u4e00\u89c4\u5219\uff0c\u53ef\u4ee5\u5b9e\u73b0\u524d\u540e\u7aef\u6570\u636e\u7684\u4ea4\u4e92\u3002<\/p>\n<ol>\n<li>\u914d\u7f6e\u63a7\u5236\u5668<\/li>\n<\/ol>\n<p>\u5728thinkphp\u4e2d\uff0c\u63a7\u5236\u5668\u662f\u524d\u540e\u7aef\u5206\u79bb\u7684\u6865\u6881\uff0c\u8d1f\u8d23\u5904\u7406\u524d\u7aef\u8bf7\u6c42\uff0c\u8c03\u7528\u540e\u7aefAPI\uff0c\u5e76\u8fd4\u56de\u6570\u636e\u3002\u5728config\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2aapi.php\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a<\/p>\n<p> return [<\/p>\n<pre> 'default_return_type' =&amp;gt; 'json'<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>];<\/p>\n<p>\u8fd9\u4e00\u914d\u7f6e\u53ef\u4ee5\u4f7f\u5f97API\u63a5\u53e3\u7684\u9ed8\u8ba4\u8fd4\u56de\u7c7b\u578b\u4e3aJSON\u683c\u5f0f\u3002<\/p>\n<p>\u7136\u540e\uff0c\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2aapi\u76ee\u5f55\uff0c\u5728api\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2av1\u76ee\u5f55\uff0c\u4f5c\u4e3aAPI\u7248\u672c\u53f7\u3002\u5728v1\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u76f8\u5e94\u7684\u63a7\u5236\u5668\u6587\u4ef6\uff0c\u4f8b\u5982\uff1a<\/p>\n<p> namespace apppi1controller;<br \/>use thinkController;<\/p>\n<p>class UserController extends Controller{<\/p>\n<pre> public function index(){\n      $data = ['name' =&amp;gt; 'Tom', 'age' =&amp;gt; '18'];\n      return json($data);\n }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>}<\/p>\n<p>\u8fd9\u4e00\u63a7\u5236\u5668\u4e2d\uff0c\u4f7f\u7528\u9759\u6001\u6570\u636e\u6a21\u62df\u540e\u7aef\u6570\u636e\uff0c\u5e76\u5c06\u5176\u4ee5JSON\u683c\u5f0f\u8fd4\u56de\u7ed9\u524d\u7aef\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\uff0c\u7f16\u5199\u4e0d\u540c\u7684\u63a7\u5236\u5668\uff0c\u5b9e\u73b0\u4e0d\u540c\u7684\u6570\u636e\u4ea4\u4e92\u65b9\u5f0f\u3002<\/p>\n<ol>\n<li>\u914d\u7f6e\u524d\u7aef\u9875\u9762<\/li>\n<\/ol>\n<p>\u5728\u524d\u7aef\u9875\u9762\u4e2d\uff0c\u9700\u8981\u8fdb\u884c\u5982\u4e0b\u914d\u7f6e\uff1a<\/p>\n<p>\uff081\uff09\u5728\u5f00\u53d1\u73af\u5883\u4e2d\uff0c\u4f7f\u7528webpack\u7b49\u6253\u5305\u5de5\u5177\u5c06\u524d\u7aef\u9879\u76ee\u751f\u6210\u9759\u6001\u6587\u4ef6\uff0c\u5e76\u653e\u7f6e\u5230thinkphp\u9879\u76ee\u7684public\u76ee\u5f55\u4e2d\u3002<\/p>\n<p>\uff082\uff09\u5728\u524d\u7aef\u8def\u7531\u8df3\u8f6c\u65f6\uff0c\u9700\u8981\u4f7f\u7528ajax\u7b49\u6280\u672f\uff0c\u5c06\u524d\u7aef\u8def\u7531\u4f20\u9012\u7ed9\u540e\u7aef\u63a5\u53e3\u3002<\/p>\n<p>\uff083\uff09\u5728\u4e0e\u540e\u7aef\u63a5\u53e3\u4ea4\u4e92\u65f6\uff0c\u9700\u8981\u9075\u5faaRESTful API\u89c4\u8303\uff0c\u4f7f\u7528GET\u3001POST\u3001PUT\u7b49\u52a8\u8bcd\u5bf9\u8d44\u6e90\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<ol>\n<li>\u6d4b\u8bd5\u63a5\u53e3<\/li>\n<\/ol>\n<p>\u5728\u6240\u6709\u914d\u7f6e\u90fd\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u8fdb\u884c\u63a5\u53e3\u6d4b\u8bd5\u3002\u5f00\u53d1\u8005\u53ef\u4ee5\u4f7f\u7528postman\u7b49\u5de5\u5177\uff0c\u6a21\u62df\u524d\u7aef\u53d1\u9001\u8bf7\u6c42\uff0c\u5e76\u67e5\u770b\u8fd4\u56de\u7ed3\u679c\u3002\u5982\u679c\u8fd4\u56de\u7684\u662f\u9884\u671f\u7684JSON\u6570\u636e\uff0c\u5219\u8bf4\u660e\u524d\u540e\u7aef\u5206\u79bb\u6280\u672f\u5df2\u7ecf\u6210\u529f\u5e94\u7528\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>thinkphp\u662f\u4e00\u6b3e\u529f\u80fd\u5f3a\u5927\u7684PHP\u6846\u67b6\uff0c\u652f\u6301\u524d\u540e\u7aef\u5206\u79bb\u6280\u672f\u3002\u901a\u8fc7\u672c\u6587\u4e2d\u6240\u4ecb\u7ecd\u7684\u914d\u7f6e\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5c06\u81ea\u5df1\u7684\u9879\u76ee\u8f6c\u6362\u4e3a\u524d\u540e\u7aef\u5206\u79bb\u7684\u67b6\u6784\uff0c\u63d0\u9ad8\u9879\u76ee\u7684\u53ef\u7ef4\u62a4\u6027\u3001\u53ef\u6269\u5c55\u6027\u548c\u53ef\u91cd\u7528\u6027\u3002\u540c\u65f6\uff0c\u4e5f\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u5e94\u5bf9\u5e02\u573a\u7ade\u4e89\u548c\u6280\u672f\u53d8\u9769\uff0c\u63d0\u9ad8\u9879\u76ee\u7684\u7ade\u4e89\u529b\u548c\u964d\u4f4e\u5f00\u53d1\u6210\u672c\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fthinkphp\u524d\u540e\u7aef\u5206\u79bb\u600e\u4e48\u914d\u7f6e\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\u524d\u540e\u7aef\u5206\u79bb\u6280\u672f\u7684\u666e\u53ca\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u5f00\u53d1\u8005\u5f00\u59cb\u5c1d\u8bd5\u5c06\u81ea\u5df1\u7684\u9879\u76ee\u4ece\u4f20\u7edf\u7684mvc\u67b6\u6784\u8f6c\u6362\u4e3a\u524d\u540e\u7aef\u5206\u79bb\u7684\u67b6\u6784\u3002\u800cthinkphp\u4f5c\u4e3a\u4e00\u6b3e\u5f00\u6e90\u7684php\u6846\u67b6\uff0c\u4e5f\u79ef\u6781\u54cd\u5e94\u8fd9\u4e00\u6280\u672f\u6f6e\u6d41\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u914d\u7f6ethinkphp\u7684\u524d\u540e\u7aef\u5206\u79bb\u6a21\u5f0f\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u9879\u76ee\u4e2d\u5e94\u7528\u8be5\u6280\u672f\u3002 \u73af\u5883\u914d\u7f6e \u5728\u4f7f\u7528thinkphp\u5b9e\u73b0\u524d\u540e\u7aef\u5206\u79bb\u4e4b\u524d\uff0c\u9700\u8981\u5148\u8fdb\u884c\u73af\u5883\u914d\u7f6e\u3002\u9996\u5148\uff0c\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\uff0c\u4f7f\u7528composer\u5b89\u88c5thinkphp\u6846\u67b6\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a composer create-project topthink\/think \u7136\u540e\uff0c\u5728Apache\u6216Nginx\u4e2d\u914d\u7f6e\u865a\u62df\u4e3b\u673a\uff0c\u5c06\u9879\u76ee\u6839\u76ee\u5f55\u8bbe\u7f6e\u4e3a\u7f51\u7ad9\u6839\u76ee\u5f55\u3002\u540c\u65f6\uff0c\u8fd8\u9700\u8981\u5728PHP\u914d\u7f6e\u6587\u4ef6\u4e2d\u542f\u7528rewrite\u6a21\u5757\uff0c\u4f7f\u5f97thinkphp\u80fd\u591f\u6b63\u5e38\u4f7f\u7528\u8def\u7531\u529f\u80fd\u3002 \u914d\u7f6e\u8def\u7531 \u5728thinkphp\u4e2d\uff0c\u8def\u7531\u914d\u7f6e\u662f\u5173\u952e\u6b65\u9aa4\u4e4b\u4e00\uff0c\u4e5f\u662f\u524d\u540e\u7aef\u5206\u79bb\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u627e\u5230config\u76ee\u5f55\u4e2d\u7684route.php\u6587\u4ef6\uff0c\u5c06\u5176\u6253\u5f00\u3002\u7136\u540e\uff0c\u6839\u636e\u524d\u540e\u7aef\u5206\u79bb\u7684\u9700\u6c42\uff0c\u8fdb\u884c\u5982\u4e0b\u914d\u7f6e\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \uff081\uff09\u5c06\u9ed8\u8ba4\u7684\u8def\u7531\u914d\u7f6e\u6539\u4e3a\uff1a &#8216;\/&#8217; =&gt; &#8216;index\/index\/index&#8217; \u8fd9\u4e00\u6539\u52a8\u53ef\u4ee5\u4f7f\u5f97\u9ed8\u8ba4\u8bbf\u95ee\u7684\u9875\u9762\u53d8\u4e3a\u524d\u7aef\u9875\u9762\uff0c\u800c\u975e\u540e\u7aef\u9875\u9762\u3002 \uff082\uff09\u5728\u8def\u7531\u914d\u7f6e\u4e2d\u6dfb\u52a0\u524d\u7aef\u9875\u9762\u7684\u8def\u7531\u89c4\u5219\uff0c\u4f8b\u5982\uff1a &#8216;@^\/:path$&#8217; =&gt; &#8216;index\/index\/index&#8217; \u5176\u4e2d\uff0c:path\u8868\u793a\u524d\u7aef\u9875\u9762\u7684\u8def\u5f84\u3002\u901a\u8fc7\u8fd9\u4e00\u89c4\u5219\uff0c\u53ef\u4ee5\u5b9e\u73b0\u524d\u7aef\u8def\u7531\u7684\u8df3\u8f6c\u3002 \uff083\uff09\u5728\u8def\u7531\u914d\u7f6e\u4e2d\u6dfb\u52a0\u63a5\u53e3\u7684\u8def\u7531\u89c4\u5219\uff0c\u4f8b\u5982\uff1a &#8216;@^api\/:controller\/:action$&#8217; =&gt; &#8216;api\/:controller\/:action&#8217; \u5176\u4e2d\uff0c:controller\u8868\u793a\u63a7\u5236\u5668\u540d\uff0c:action\u8868\u793a\u65b9\u6cd5\u540d\u3002\u901a\u8fc7\u8fd9\u4e00\u89c4\u5219\uff0c\u53ef\u4ee5\u5b9e\u73b0\u524d\u540e\u7aef\u6570\u636e\u7684\u4ea4\u4e92\u3002 \u914d\u7f6e\u63a7\u5236\u5668 \u5728thinkphp\u4e2d\uff0c\u63a7\u5236\u5668\u662f\u524d\u540e\u7aef\u5206\u79bb\u7684\u6865\u6881\uff0c\u8d1f\u8d23\u5904\u7406\u524d\u7aef\u8bf7\u6c42\uff0c\u8c03\u7528\u540e\u7aefAPI\uff0c\u5e76\u8fd4\u56de\u6570\u636e\u3002\u5728config\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2aapi.php\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u914d\u7f6e\uff1a return [ &#8216;default_return_type&#8217; =&amp;gt; &#8216;json&#8217; \u767b\u5f55\u540e\u590d\u5236 ]; \u8fd9\u4e00\u914d\u7f6e\u53ef\u4ee5\u4f7f\u5f97API\u63a5\u53e3\u7684\u9ed8\u8ba4\u8fd4\u56de\u7c7b\u578b\u4e3aJSON\u683c\u5f0f\u3002 \u7136\u540e\uff0c\u5728\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2aapi\u76ee\u5f55\uff0c\u5728api\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u4e00\u4e2av1\u76ee\u5f55\uff0c\u4f5c\u4e3aAPI\u7248\u672c\u53f7\u3002\u5728v1\u76ee\u5f55\u4e0b\uff0c\u521b\u5efa\u76f8\u5e94\u7684\u63a7\u5236\u5668\u6587\u4ef6\uff0c\u4f8b\u5982\uff1a namespace apppi1controller;use thinkController; class UserController extends Controller{ public function index(){ $data = [&#8216;name&#8217; =&amp;gt; &#8216;Tom&#8217;, &#8216;age&#8217; =&amp;gt; &#8217;18&#8217;]; return [&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-22700","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22700","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=22700"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/22700\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=22700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=22700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=22700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}