{"id":23678,"date":"2024-11-21T14:14:13","date_gmt":"2024-11-21T06:14:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23678\/"},"modified":"2024-11-21T14:14:13","modified_gmt":"2024-11-21T06:14:13","slug":"%e5%88%a9%e7%94%a8thinkphp6%e5%ae%9e%e7%8e%b0api%e6%96%87%e6%a1%a3%e8%87%aa%e5%8a%a8%e7%94%9f%e6%88%90","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23678\/","title":{"rendered":"\u5229\u7528ThinkPHP6\u5b9e\u73b0API\u6587\u6863\u81ea\u52a8\u751f\u6210"},"content":{"rendered":"<p>\u968f\u7740api\u7684\u5e94\u7528\u8d8a\u6765\u8d8a\u5e7f\u6cdb\uff0c\u81ea\u52a8\u751f\u6210api\u6587\u6863\u6210\u4e3a\u4e86\u4e00\u4e2a\u5fc5\u4e0d\u53ef\u5c11\u7684\u5de5\u5177\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u75286\u6846\u67b6\u81ea\u52a8\u751f\u6210api\u6587\u6863\u3002<\/p>\n<p>\u4e00\u3001ThinkPHP6\u6846\u67b6\u4ecb\u7ecd<\/p>\n<p>ThinkPHP6\u662f\u4e00\u4e2a\u4f7f\u7528PHP\u8bed\u8a00\u5f00\u53d1\u7684\u9ad8\u6548\u3001\u7b80\u5355\u3001\u65b9\u4fbf\u3001\u7075\u6d3b\u7684\u5f00\u6e90\u6846\u67b6\u3002\u5b83\u91c7\u7528\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u5f00\u53d1\u6a21\u5f0f\uff0c\u652f\u6301MVC\uff08\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\uff09\u67b6\u6784\uff0c\u5177\u6709\u8def\u7531\u3001\u7f13\u5b58\u3001\u9a8c\u8bc1\u3001\u6a21\u677f\u5f15\u64ce\u7b49\u5f3a\u5927\u529f\u80fd\u3002<\/p>\n<p>\u4e8c\u3001\u5b89\u88c5Swagger UI<\/p>\n<p>Swagger\u662f\u4e00\u79cdAPI\u6587\u6863\u81ea\u52a8\u751f\u6210\u5de5\u5177\uff0c\u5b83\u80fd\u591f\u81ea\u52a8\u751f\u6210API\u7684\u6587\u6863\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u4e00\u4e2aWeb\u754c\u9762\u6765\u6f14\u793aAPI\u7684\u6267\u884c\u7ed3\u679c\u3002\u5728\u4f7f\u7528ThinkPHP6\u6765\u5b9e\u73b0API\u6587\u6863\u81ea\u52a8\u751f\u6210\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5Swagger\u3002<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Composer\u5de5\u5177\u6765\u5b89\u88c5Swagger\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\uff1a<\/p>\n<pre>composer require zircote\/swagger-php<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\u521b\u5efaSwagger\u914d\u7f6e\u6587\u4ef6\uff0c\u547d\u540d\u4e3aswagger.php\uff1a<\/p>\n<pre>&lt;?php return [\n    'swagger' =&gt; [\n        'api' =&amp;gt; [\n            'title' =&amp;gt; 'API\u6587\u6863',  \/\/API\u6587\u6863\u7684\u6807\u9898\n        ],\n        'paths' =&amp;gt; [\n            APP_PATH . '\/',\n        ],\n        'exclude' =&amp;gt; [\n        ],\n        'swagger-ui' =&amp;gt; [\n            'title' =&amp;gt; 'API\u6587\u6863',  \/\/API\u6587\u6863\u7684\u6807\u9898\n        ],\n        'securityDefinitions' =&amp;gt; [\n        ],\n    ],\n];<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e09\u3001\u5b9a\u4e49API\u6587\u6863\u6ce8\u91ca<\/p>\n<p>\u4e3a\u4e86\u8ba9Swagger\u80fd\u591f\u81ea\u52a8\u8bc6\u522b\u548c\u751f\u6210API\u6587\u6863\uff0c\u6211\u4eec\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u76f8\u5e94\u7684\u6ce8\u91ca\u3002ThinkPHP6\u63d0\u4f9b\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6ce8\u91ca\u683c\u5f0f\uff0c\u7528\u4e8e\u5b9a\u4e49API\u6587\u6863\u3002<\/p>\n<p>\u5728\u63a7\u5236\u5668\u4e2d\u5b9a\u4e49API\u6587\u6863\u6ce8\u91ca\uff1a<\/p>\n<pre>&lt;?php declare(strict_types=1);\n\nnamespace appcontroller;\n\nclass Example\n{\n    \/**\n     * @OAGet(\n     *      path=\"\/example\/index\",\n     *      operationId=\"exampleIndex\",\n     *      tags={\"Example\"},\n     *      summary=\"\u793a\u4f8b\u63a5\u53e3\",\n     *      description=\"\u8fd9\u662f\u4e00\u4e2a\u793a\u4f8b\u63a5\u53e3\",\n     *      @OAResponse(\n     *          response=200,\n     *          description=\"\u64cd\u4f5c\u6210\u529f\",\n     *      ),\n     *      @OAResponse(\n     *          response=401,\n     *          description=\"\u672a\u6388\u6743\",\n     *      ),\n     *      security={\n     *          {\"Bearer\": {}}\n     *      }\n     * )\n     *\/\n    public function index()\n    {\n        \/\/\u63a5\u53e3\u4ee3\u7801\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c@OA\u5f00\u5934\u7684\u6ce8\u91ca\u6807\u7b7e\u88ab\u89e3\u6790\u4e3aSwagger\u7684\u89c4\u8303\u683c\u5f0f\u3002\u5176\u4e2d\uff0c@OAGet\u5b9a\u4e49\u4e86API\u7684\u8bf7\u6c42\u65b9\u5f0f\u4e3aGet\u65b9\u6cd5\uff1bpath\u5b9a\u4e49\u4e86API\u7684\u8def\u5f84\uff1boperationId\u5b9a\u4e49\u4e86\u64cd\u4f5c\u7684id\uff1btags\u5b9a\u4e49\u4e86API\u6240\u5c5e\u7684\u6807\u7b7e\uff1bsummary\u5b9a\u4e49\u4e86API\u7684\u6982\u8ff0\uff1bdescription\u5b9a\u4e49\u4e86API\u7684\u8be6\u7ec6\u63cf\u8ff0\uff1b@OAResponse\u5b9a\u4e49\u4e86API\u7684\u54cd\u5e94\u7ed3\u679c\u53ca\u72b6\u6001\u7801\uff1bsecurity\u5b9a\u4e49\u4e86API\u7684\u8bbf\u95ee\u6743\u9650\u3002<\/p>\n<p>\u56db\u3001\u751f\u6210API\u6587\u6863<\/p>\n<p>\u5728\u5b9a\u4e49\u597dAPI\u6587\u6863\u6ce8\u91ca\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Swagger\u6765\u751f\u6210API\u6587\u6863\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php think swagger:export --output public\/swagger.json<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be5\u547d\u4ee4\u4f1a\u5c06API\u6587\u6863\u4fdd\u5b58\u5230public\u76ee\u5f55\u4e0b\u7684swagger.json\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>\u4e94\u3001\u8bbf\u95eeAPI\u6587\u6863<\/p>\n<p>\u4f7f\u7528Swagger UI\u6765\u5c55\u793aAPI\u6587\u6863\u3002\u6211\u4eec\u53ef\u4ee5\u5c06Swagger UI\u9879\u76ee\u90e8\u7f72\u5230Web\u670d\u52a1\u5668\u4e2d\uff0c\u6216\u8005\u5728\u672c\u5730\u8fd0\u884c\u3002<\/p>\n<p>\u5728\u672c\u5730\u8fd0\u884c\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u547d\u4ee4\u5feb\u901f\u542f\u52a8\u4e00\u4e2aSwagger UI\u670d\u52a1\uff1a<\/p>\n<pre>docker run --rm -p 8080:8080 -e SWAGGER_JSON=\/data\/swagger.json -v \/path\/to\/swagger.json:\/data\/swagger.json swaggerapi\/swagger-ui<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0c\/path\/to\/swagger.json\u662fswagger.json\u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<p>\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95eehttp:\/\/localhost:8080\u5373\u53ef\u67e5\u770bAPI\u6587\u6863\u3002<\/p>\n<p>\u516d\u3001\u603b\u7ed3<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u5229\u7528ThinkPHP6\u6846\u67b6\u548cSwagger\u81ea\u52a8\u751f\u6210API\u6587\u6863\u3002\u81ea\u52a8\u751f\u6210API\u6587\u6863\u53ef\u4ee5\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff0c\u964d\u4f4e\u7ef4\u62a4\u6210\u672c\u3002\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u76f8\u4fe1\u8bfb\u8005\u5df2\u7ecf\u80fd\u591f\u719f\u7ec3\u5730\u8fd0\u7528ThinkPHP6\u6846\u67b6\u548cSwagger\u6765\u5b9e\u73b0API\u6587\u6863\u7684\u81ea\u52a8\u751f\u6210\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5229\u7528ThinkPHP6\u5b9e\u73b0API\u6587\u6863\u81ea\u52a8\u751f\u6210\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\u7740api\u7684\u5e94\u7528\u8d8a\u6765\u8d8a\u5e7f\u6cdb\uff0c\u81ea\u52a8\u751f\u6210api\u6587\u6863\u6210\u4e3a\u4e86\u4e00\u4e2a\u5fc5\u4e0d\u53ef\u5c11\u7684\u5de5\u5177\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u75286\u6846\u67b6\u81ea\u52a8\u751f\u6210api\u6587\u6863\u3002 \u4e00\u3001ThinkPHP6\u6846\u67b6\u4ecb\u7ecd ThinkPHP6\u662f\u4e00\u4e2a\u4f7f\u7528PHP\u8bed\u8a00\u5f00\u53d1\u7684\u9ad8\u6548\u3001\u7b80\u5355\u3001\u65b9\u4fbf\u3001\u7075\u6d3b\u7684\u5f00\u6e90\u6846\u67b6\u3002\u5b83\u91c7\u7528\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u5f00\u53d1\u6a21\u5f0f\uff0c\u652f\u6301MVC\uff08\u6a21\u578b-\u89c6\u56fe-\u63a7\u5236\u5668\uff09\u67b6\u6784\uff0c\u5177\u6709\u8def\u7531\u3001\u7f13\u5b58\u3001\u9a8c\u8bc1\u3001\u6a21\u677f\u5f15\u64ce\u7b49\u5f3a\u5927\u529f\u80fd\u3002 \u4e8c\u3001\u5b89\u88c5Swagger UI Swagger\u662f\u4e00\u79cdAPI\u6587\u6863\u81ea\u52a8\u751f\u6210\u5de5\u5177\uff0c\u5b83\u80fd\u591f\u81ea\u52a8\u751f\u6210API\u7684\u6587\u6863\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u4e00\u4e2aWeb\u754c\u9762\u6765\u6f14\u793aAPI\u7684\u6267\u884c\u7ed3\u679c\u3002\u5728\u4f7f\u7528ThinkPHP6\u6765\u5b9e\u73b0API\u6587\u6863\u81ea\u52a8\u751f\u6210\u65f6\uff0c\u6211\u4eec\u9700\u8981\u5148\u5b89\u88c5Swagger\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7Composer\u5de5\u5177\u6765\u5b89\u88c5Swagger\u3002\u5728\u547d\u4ee4\u884c\u4e2d\u8f93\u5165\uff1a composer require zircote\/swagger-php \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e0b\u521b\u5efaSwagger\u914d\u7f6e\u6587\u4ef6\uff0c\u547d\u540d\u4e3aswagger.php\uff1a &lt;?php return [ &#8216;swagger&#8217; =&gt; [ &#8216;api&#8217; =&amp;gt; [ &#8216;title&#8217; =&amp;gt; &#8216;API\u6587\u6863&#8217;, \/\/API\u6587\u6863\u7684\u6807\u9898 ], &#8216;paths&#8217; =&amp;gt; [ APP_PATH . &#8216;\/&#8217;, ], &#8216;exclude&#8217; =&amp;gt; [ ], &#8216;swagger-ui&#8217; =&amp;gt; [ &#8216;title&#8217; =&amp;gt; &#8216;API\u6587\u6863&#8217;, \/\/API\u6587\u6863\u7684\u6807\u9898 ], &#8216;securityDefinitions&#8217; =&amp;gt; [ ], ], ]; \u767b\u5f55\u540e\u590d\u5236 \u4e09\u3001\u5b9a\u4e49API\u6587\u6863\u6ce8\u91ca \u4e3a\u4e86\u8ba9Swagger\u80fd\u591f\u81ea\u52a8\u8bc6\u522b\u548c\u751f\u6210API\u6587\u6863\uff0c\u6211\u4eec\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u76f8\u5e94\u7684\u6ce8\u91ca\u3002ThinkPHP6\u63d0\u4f9b\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u6ce8\u91ca\u683c\u5f0f\uff0c\u7528\u4e8e\u5b9a\u4e49API\u6587\u6863\u3002 [&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-23678","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23678","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=23678"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23678\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23678"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23678"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23678"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}