{"id":23665,"date":"2024-11-21T09:54:17","date_gmt":"2024-11-21T01:54:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23665\/"},"modified":"2024-11-21T09:54:17","modified_gmt":"2024-11-21T01:54:17","slug":"%e5%88%a9%e7%94%a8thinkphp6%e5%ae%9e%e7%8e%b0restful-api","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23665\/","title":{"rendered":"\u5229\u7528ThinkPHP6\u5b9e\u73b0RESTful API"},"content":{"rendered":"<p>\u968f\u7740\u79fb\u52a8\u4e92\u8054\u7f51\u7684\u4e0d\u65ad\u53d1\u5c55\uff0crestful api\u6210\u4e3a\u4e86web\u5f00\u53d1\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u5b83\u662f\u4e00\u79cd\u57fa\u4e8ehttp\u534f\u8bae\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u8bbf\u95ee\u548c\u64cd\u4f5cweb\u8d44\u6e90\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u5f00\u53d1restful api\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528php\u6846\u67b66\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5efa\u7acb\u4e00\u4e2a\u57fa\u672c\u7684RESTful API\u7ed3\u6784\u3002\u4f7f\u7528ThinkPHP6\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u751f\u6210\u4e00\u4e2aRESTful API\u5e94\u7528\u7a0b\u5e8f\u3002\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\uff0c\u5207\u6362\u5230\u6211\u4eec\u7684\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php think build --name api<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0capi\u662f\u6211\u4eec\u8981\u521b\u5efa\u7684\u5e94\u7528\u7a0b\u5e8f\u540d\u79f0\u3002\u6267\u884c\u6b64\u547d\u4ee4\u540e\uff0cThinkPHP6\u4f1a\u4e3a\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684RESTful API\u5e94\u7528\u7a0b\u5e8f\u7ed3\u6784\uff0c\u5305\u542b\u4ee5\u4e0b\u76ee\u5f55\u548c\u6587\u4ef6\uff1a<\/p>\n<pre>api\/\n\u251c\u2500 app\/\n\u2502  \u251c\u2500 controller\/\n\u2502  \u251c\u2500 model\/\n\u2502  \u251c\u2500 service\/\n\u2502  \u251c\u2500 validate\/\n\u2502  \u2514\u2500 route.php\n\u251c\u2500 config\/\n\u2502  \u251c\u2500 app.php\n\u2502  \u2514\u2500 database.php\n\u251c\u2500 public\/\n\u2502  \u251c\u2500 index.php\n\u2502  \u2514\u2500 .htaccess\n\u251c\u2500 vendor\/\n\u251c\u2500 .env\n\u251c\u2500 composer.json\n\u2514\u2500 README.md<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0capp\u76ee\u5f55\u5b58\u653e\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u76f8\u5173\u6587\u4ef6\u3002config\u76ee\u5f55\u5b58\u653e\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u914d\u7f6e\u6587\u4ef6\u3002public\u76ee\u5f55\u5b58\u653e\u6211\u4eec\u7684\u5165\u53e3\u6587\u4ef6\u548c\u9759\u6001\u8d44\u6e90\u6587\u4ef6\u3002vendor\u76ee\u5f55\u4fdd\u5b58\u4e86\u6211\u4eec\u7684Composer\u4f9d\u8d56\u5305\u3002.env\u662f\u6211\u4eec\u7684\u73af\u5883\u914d\u7f6e\u6587\u4ef6\u3002composer.json\u5219\u662f\u6211\u4eec\u7684Composer\u914d\u7f6e\u6587\u4ef6\u3002README.md\u5219\u662f\u6211\u4eec\u7684\u8bf4\u660e\u6587\u6863\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u6211\u4eec\u7684API\u8def\u7531\u89c4\u5219\u3002\u5728app\u76ee\u5f55\u4e0b\u7684route.php\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u6dfb\u52a0\u6211\u4eec\u7684API\u8def\u7531\u89c4\u5219\u3002\u4f8b\u5982\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<pre>Route::resource('article', 'ArticleController');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u8fd9\u884c\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2aarticle\u8d44\u6e90\u8def\u7531\uff0c\u610f\u601d\u662f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u8def\u7531\u6765\u8bbf\u95ee\u548c\u64cd\u4f5cArticle\u8d44\u6e90\u3002\u8fd9\u4e2a\u8def\u7531\u4f1a\u81ea\u52a8\u751f\u62107\u4e2aRESTful API\u52a8\u4f5c\uff0c\u5305\u62ecindex\u3001create\u3001store\u3001show\u3001edit\u3001update\u548cdestroy\u3002\u6211\u4eec\u53ef\u4ee5\u5728ArticleController\u4e2d\u5b9e\u73b0\u8fd9\u4e9b\u52a8\u4f5c\u3002<\/p>\n<pre>&lt;?php namespace appcontroller;\n\nuse thinkRequest;\nuse appmodelArticle as ArticleModel;\n\nclass ArticleController\n{\n    public function index()\n    {\n        $articles = ArticleModel::select();\n        return json($articles);\n    }\n\n    public function create()\n    {\n        return 'create';\n    }\n\n    public function store(Request $request)\n    {\n        $data = $request-&gt;param();\n        $article = ArticleModel::create($data);\n        return json($article);\n    }\n\n    public function show($id)\n    {\n        $article = ArticleModel::find($id);\n        return json($article);\n    }\n\n    public function edit($id)\n    {\n        return 'edit';\n    }\n\n    public function update(Request $request, $id)\n    {\n        $data = $request-&amp;gt;param();\n        $article = ArticleModel::update($data, ['id' =&amp;gt; $id]);\n        return json($article);\n    }\n\n    public function destroy($id)\n    {\n        $article = ArticleModel::destroy($id);\n        return json($article);\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7528ArticleModel\u6765\u5904\u7406\u4e0eArticle\u8d44\u6e90\u6709\u5173\u7684\u6570\u636e\u64cd\u4f5c\u3002\u5728index\u52a8\u4f5c\u4e2d\uff0c\u6211\u4eec\u83b7\u53d6\u6240\u6709\u7684Article\u6570\u636e\u5e76\u8fd4\u56de\u3002\u5728store\u52a8\u4f5c\u4e2d\uff0c\u6211\u4eec\u5c06\u901a\u8fc7Request\u5bf9\u8c61\u83b7\u53d6\u5230\u7684\u6570\u636e\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\u3002\u5176\u4ed6\u52a8\u4f5c\u7684\u5b9e\u73b0\u4e5f\u7c7b\u4f3c\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u5728config\u76ee\u5f55\u4e0b\u7684app.php\u6587\u4ef6\u4e2d\u8bbe\u7f6e\u6211\u4eec\u7684API\u5e94\u7528\u7a0b\u5e8f\u7684\u914d\u7f6e\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>return [\n    'app_status' =&amp;gt; 'api',\n    'default_return_type' =&amp;gt; 'json',\n    'http_exception_template' =&amp;gt; [\n        401 =&amp;gt; function ($request) {\n            return json(['code' =&amp;gt; 401, 'msg' =&amp;gt; 'Unauthorized']);\n        },\n        404 =&amp;gt; function ($request) {\n            return json(['code' =&amp;gt; 404, 'msg' =&amp;gt; 'Not Found']);\n        },\n        500 =&amp;gt; function ($request, $exception) {\n            return json(['code' =&amp;gt; 500, 'msg' =&amp;gt; 'Internal Server Error']);\n        },\n    ],\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6307\u5b9a\u4e86\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u54cd\u5e94\u7c7b\u578b\u4e3aJSON\u3002\u8fd8\u5b9a\u4e49\u4e86\u4e00\u4e9bHTTP\u9519\u8bef\u7684\u5904\u7406\u65b9\u5f0f\u3002<\/p>\n<p>\u81f3\u6b64\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528ThinkPHP6\u6765\u5f00\u53d1RESTful API\u3002\u4f7f\u7528\u6b64\u6846\u67b6\u53ef\u4ee5\u6781\u5927\u5730\u52a0\u5feb\u6211\u4eec\u5f00\u53d1RESTful API\u7684\u6548\u7387\uff0c\u540c\u65f6\uff0c\u5b83\u4e5f\u4e3a\u6211\u4eec\u7684API\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u4e86\u66f4\u597d\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5229\u7528ThinkPHP6\u5b9e\u73b0RESTful API\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\u79fb\u52a8\u4e92\u8054\u7f51\u7684\u4e0d\u65ad\u53d1\u5c55\uff0crestful api\u6210\u4e3a\u4e86web\u5f00\u53d1\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002\u5b83\u662f\u4e00\u79cd\u57fa\u4e8ehttp\u534f\u8bae\u7684\u901a\u4fe1\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e8e\u8bbf\u95ee\u548c\u64cd\u4f5cweb\u8d44\u6e90\u3002\u4e3a\u4e86\u66f4\u597d\u5730\u5f00\u53d1restful api\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528php\u6846\u67b66\u6765\u5b9e\u73b0\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5efa\u7acb\u4e00\u4e2a\u57fa\u672c\u7684RESTful API\u7ed3\u6784\u3002\u4f7f\u7528ThinkPHP6\u7684\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u751f\u6210\u4e00\u4e2aRESTful API\u5e94\u7528\u7a0b\u5e8f\u3002\u6253\u5f00\u547d\u4ee4\u884c\u754c\u9762\uff0c\u5207\u6362\u5230\u6211\u4eec\u7684\u9879\u76ee\u76ee\u5f55\u4e0b\uff0c\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a php think build &#8211;name api \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0capi\u662f\u6211\u4eec\u8981\u521b\u5efa\u7684\u5e94\u7528\u7a0b\u5e8f\u540d\u79f0\u3002\u6267\u884c\u6b64\u547d\u4ee4\u540e\uff0cThinkPHP6\u4f1a\u4e3a\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684RESTful API\u5e94\u7528\u7a0b\u5e8f\u7ed3\u6784\uff0c\u5305\u542b\u4ee5\u4e0b\u76ee\u5f55\u548c\u6587\u4ef6\uff1a api\/ \u251c\u2500 app\/ \u2502 \u251c\u2500 controller\/ \u2502 \u251c\u2500 model\/ \u2502 \u251c\u2500 service\/ \u2502 \u251c\u2500 validate\/ \u2502 \u2514\u2500 route.php \u251c\u2500 config\/ \u2502 \u251c\u2500 app.php \u2502 \u2514\u2500 database.php \u251c\u2500 public\/ \u2502 \u251c\u2500 index.php \u2502 \u2514\u2500 .htaccess \u251c\u2500 vendor\/ \u251c\u2500 .env \u251c\u2500 composer.json \u2514\u2500 [&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-23665","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23665","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=23665"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23665\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}