{"id":24701,"date":"2024-11-21T13:28:17","date_gmt":"2024-11-21T05:28:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24701\/"},"modified":"2024-11-21T13:28:17","modified_gmt":"2024-11-21T05:28:17","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8hyperf%e6%a1%86%e6%9e%b6%e8%bf%9b%e8%a1%8capi%e6%96%87%e6%a1%a3%e7%94%9f%e6%88%90","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24701\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884cAPI\u6587\u6863\u751f\u6210"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169776148054002.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884cAPI\u6587\u6863\u751f\u6210\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884cAPI\u6587\u6863\u751f\u6210\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884cAPI\u6587\u6863\u751f\u6210<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u968f\u7740\u4e92\u8054\u7f51\u7684\u5feb\u901f\u53d1\u5c55\uff0cAPI\uff08Application Programming Interface\uff09\u5df2\u7ecf\u6210\u4e3a\u4e86\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e0d\u540c\u7684\u5e94\u7528\u7a0b\u5e8f\u8fde\u63a5\u8d77\u6765\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u5171\u4eab\u4e0e\u4ea4\u4e92\u3002\u5bf9\u4e8e\u5f00\u53d1\u56e2\u961f\u6765\u8bf4\uff0c\u826f\u597d\u7684API\u6587\u6863\u662f\u4fdd\u8bc1\u56e2\u961f\u534f\u4f5c\u7684\u91cd\u8981\u5de5\u5177\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Hyperf\u6846\u67b6\u6765\u751f\u6210\u6e05\u6670\u3001\u6613\u7528\u7684API\u6587\u6863\uff0c\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8fdb\u884c\u5c55\u793a\u3002<\/p>\n<p>\u4e00\u3001\u51c6\u5907\u5de5\u4f5c<br \/>\u5728\u5f00\u59cb\u4f7f\u7528Hyperf\u6846\u67b6\u751f\u6210API\u6587\u6863\u4e4b\u524d\uff0c\u9700\u8981\u8fdb\u884c\u4ee5\u4e0b\u51c6\u5907\u5de5\u4f5c\uff1a<\/p>\n<ol>\n<li>\u5b89\u88c5Hyperf\u6846\u67b6\uff1a\u4f7f\u7528Composer\u5de5\u5177\u53ef\u4ee5\u7b80\u5355\u5feb\u6377\u5730\u5b89\u88c5Hyperf\u6846\u67b6\u3002<\/li>\n<li>\u914d\u7f6e\u8def\u7531\uff1a\u5728config\/routes.php\u6587\u4ef6\u4e2d\u914d\u7f6e\u8def\u7531\u4fe1\u606f\u3002<\/li>\n<li>\u5b89\u88c5API\u6587\u6863\u751f\u6210\u5de5\u5177\uff1aHyperf\u6846\u67b6\u6709\u4e00\u4e2a\u5b98\u65b9\u63a8\u8350\u7684API\u6587\u6863\u751f\u6210\u5de5\u5177\uff0c\u79f0\u4e3aSwaggervel\uff0c\u53ef\u4ee5\u901a\u8fc7Composer\u8fdb\u884c\u5b89\u88c5\u3002<\/li>\n<\/ol>\n<p>\u4e8c\u3001\u751f\u6210API\u6587\u6863<br \/>\u4ee5\u4e0b\u662f\u4f7f\u7528Hyperf\u6846\u67b6\u751f\u6210API\u6587\u6863\u7684\u5177\u4f53\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<ol>\n<li>\n<p>\u5b89\u88c5Swaggervel<\/p>\n<pre>composer require overtrue\/laravel-swagger<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a\u6587\u6863\u751f\u6210\u5668\u7c7b<br \/>\u5728app\/Doc\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2aDocGenerator.php\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php namespace AppDoc;\n\nuse HyperfValidationContractValidatorFactoryInterface;\nuse OvertrueLaravelSwaggerRequest;\nuse OvertrueLaravelSwaggerSwagger as BaseSwagger;\n\nclass DocGenerator\n{\n protected $validator;\n\n public function __construct(ValidatorFactoryInterface $validator)\n {\n     $this-&gt;validator = $validator;\n }\n\n public function generate()\n {\n     $swagger = new BaseSwagger([\n         'swagger' =&amp;gt; '2.0',\n         'info' =&amp;gt; [\n             'title' =&amp;gt; config('app.name'),\n             'version' =&amp;gt; config('app.version'),\n         ],\n     ]);\n\n     $routes = app('router')-&amp;gt;getRoutes();\n\n     foreach ($routes as $route) {\n         $methods = $route-&amp;gt;methods();\n         $path = $route-&amp;gt;uri();\n\n         foreach ($methods as $method) {\n             $request = new Request([\n                 'method' =&amp;gt; $method,\n                 'uri' =&amp;gt; $route-&amp;gt;uri(),\n             ]);\n\n             $docBlock = $route-&amp;gt;getAction()['doc'] ?? null; \/\/ \u4eceRoute\u4e2d\u83b7\u53d6\u6ce8\u91ca\n\n             $parameters = [];\n\n             $validator = $this-&amp;gt;validator-&amp;gt;make($request-&amp;gt;all(), $docBlock ? $docBlock['rules'] : []);\n\n             foreach ($validator-&amp;gt;failed() as $field =&amp;gt; $messages) {\n                 $parameters[] = [\n                     'name' =&amp;gt; $field,\n                     'in' =&amp;gt; 'query',\n                     'required' =&amp;gt; true,\n                     'description' =&amp;gt; implode(', ', $messages),\n                 ];\n             }\n\n             $responses = [];\n\n             $responses[] = [\n                 'statusCode' =&amp;gt; 200,\n                 'description' =&amp;gt; '\u8bf7\u6c42\u6210\u529f',\n                 'data' =&amp;gt; [\n                     'type' =&amp;gt; 'object',\n                     'properties' =&amp;gt; [\n                         'code' =&amp;gt; [\n                             'type' =&amp;gt; 'integer',\n                         ],\n                         'message' =&amp;gt; [\n                             'type' =&amp;gt; 'string',\n                         ],\n                         'data' =&amp;gt; [\n                             'type' =&amp;gt; 'object',\n                             'nullable' =&amp;gt; true,\n                         ],\n                     ],\n                 ],\n             ];\n\n             $swagger-&amp;gt;addPath($path, $method, [\n                 'parameters' =&amp;gt; $parameters,\n                 'responses' =&amp;gt; $responses,\n             ]);\n         }\n     }\n\n     return $swagger-&amp;gt;toYaml();\n }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p>\u914d\u7f6e\u8bbf\u95ee\u8def\u7531<br \/>\u5728config\/routes.php\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u8def\u7531\u914d\u7f6e\uff1a<\/p>\n<pre>use AppDocDocGenerator;\n\nRouter::get('\/api\/docs', function (DocGenerator $docGenerator) {\n return $docGenerator-&amp;gt;generate();\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<li>\n<p>\u751f\u6210API\u6587\u6863<br \/>\u5728\u7ec8\u7aef\u4e2d\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u751f\u6210API\u6587\u6863\uff1a<\/p>\n<pre>php bin\/hyperf.php serve<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/li>\n<\/ol>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884cAPI\u6587\u6863\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>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884cAPI\u6587\u6863\u751f\u6210 \u5f15\u8a00\uff1a\u968f\u7740\u4e92\u8054\u7f51\u7684\u5feb\u901f\u53d1\u5c55\uff0cAPI\uff08Application Programming Interface\uff09\u5df2\u7ecf\u6210\u4e3a\u4e86\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e0d\u540c\u7684\u5e94\u7528\u7a0b\u5e8f\u8fde\u63a5\u8d77\u6765\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u5171\u4eab\u4e0e\u4ea4\u4e92\u3002\u5bf9\u4e8e\u5f00\u53d1\u56e2\u961f\u6765\u8bf4\uff0c\u826f\u597d\u7684API\u6587\u6863\u662f\u4fdd\u8bc1\u56e2\u961f\u534f\u4f5c\u7684\u91cd\u8981\u5de5\u5177\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u5229\u7528Hyperf\u6846\u67b6\u6765\u751f\u6210\u6e05\u6670\u3001\u6613\u7528\u7684API\u6587\u6863\uff0c\u901a\u8fc7\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u6765\u8fdb\u884c\u5c55\u793a\u3002 \u4e00\u3001\u51c6\u5907\u5de5\u4f5c\u5728\u5f00\u59cb\u4f7f\u7528Hyperf\u6846\u67b6\u751f\u6210API\u6587\u6863\u4e4b\u524d\uff0c\u9700\u8981\u8fdb\u884c\u4ee5\u4e0b\u51c6\u5907\u5de5\u4f5c\uff1a \u5b89\u88c5Hyperf\u6846\u67b6\uff1a\u4f7f\u7528Composer\u5de5\u5177\u53ef\u4ee5\u7b80\u5355\u5feb\u6377\u5730\u5b89\u88c5Hyperf\u6846\u67b6\u3002 \u914d\u7f6e\u8def\u7531\uff1a\u5728config\/routes.php\u6587\u4ef6\u4e2d\u914d\u7f6e\u8def\u7531\u4fe1\u606f\u3002 \u5b89\u88c5API\u6587\u6863\u751f\u6210\u5de5\u5177\uff1aHyperf\u6846\u67b6\u6709\u4e00\u4e2a\u5b98\u65b9\u63a8\u8350\u7684API\u6587\u6863\u751f\u6210\u5de5\u5177\uff0c\u79f0\u4e3aSwaggervel\uff0c\u53ef\u4ee5\u901a\u8fc7Composer\u8fdb\u884c\u5b89\u88c5\u3002 \u4e8c\u3001\u751f\u6210API\u6587\u6863\u4ee5\u4e0b\u662f\u4f7f\u7528Hyperf\u6846\u67b6\u751f\u6210API\u6587\u6863\u7684\u5177\u4f53\u6b65\u9aa4\u548c\u4ee3\u7801\u793a\u4f8b\uff1a \u5b89\u88c5Swaggervel composer require overtrue\/laravel-swagger \u767b\u5f55\u540e\u590d\u5236 \u521b\u5efa\u4e00\u4e2a\u6587\u6863\u751f\u6210\u5668\u7c7b\u5728app\/Doc\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2aDocGenerator.php\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u7f16\u5199\u4ee5\u4e0b\u4ee3\u7801\uff1a &lt;?php namespace AppDoc; use HyperfValidationContractValidatorFactoryInterface; use OvertrueLaravelSwaggerRequest; use OvertrueLaravelSwaggerSwagger as BaseSwagger; class DocGenerator { protected $validator; public function __construct(ValidatorFactoryInterface $validator) { $this-&gt;validator = $validator; } public function generate() { $swagger = new BaseSwagger([ &#8216;swagger&#8217; =&amp;gt; &#8216;2.0&#8217;, &#8216;info&#8217; =&amp;gt; [ &#8216;title&#8217; [&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-24701","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24701","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=24701"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24701\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}