{"id":24772,"date":"2024-11-21T14:44:33","date_gmt":"2024-11-21T06:44:33","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24772\/"},"modified":"2024-11-21T14:44:33","modified_gmt":"2024-11-21T06:44:33","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8hyperf%e6%a1%86%e6%9e%b6%e8%bf%9b%e8%a1%8c%e6%8e%a5%e5%8f%a3%e6%96%87%e6%a1%a3%e7%94%9f%e6%88%90","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24772\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u63a5\u53e3\u6587\u6863\u751f\u6210"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169820687063423.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u63a5\u53e3\u6587\u6863\u751f\u6210\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u63a5\u53e3\u6587\u6863\u751f\u6210\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u63a5\u53e3\u6587\u6863\u751f\u6210<\/p>\n<p>\u968f\u7740Web\u5e94\u7528\u7a0b\u5e8f\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u63a5\u53e3\u6587\u6863\u7684\u7f16\u5199\u548c\u7ef4\u62a4\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\u3002\u63a5\u53e3\u6587\u6863\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u4e86\u89e3\u548c\u4f7f\u7528API\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u534f\u540c\u5f00\u53d1\u80fd\u529b\u3002\u5728\u65e5\u5e38\u7684\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u624b\u52a8\u7f16\u5199\u548c\u66f4\u65b0\u63a5\u53e3\u6587\u6863\uff0c\u8fd9\u662f\u4e00\u9879\u76f8\u5bf9\u7e41\u7410\u4e14\u5bb9\u6613\u51fa\u9519\u7684\u5de5\u4f5c\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Hyperf\u6846\u67b6\u7684\u81ea\u52a8\u5de5\u5177\uff0c\u901a\u8fc7\u6ce8\u89e3\u751f\u6210\u63a5\u53e3\u6587\u6863\u3002<\/p>\n<p>Hyperf\u6846\u67b6\u662f\u57fa\u4e8eSwoole\u548cHyperf\u7ec4\u4ef6\u7684\u9ad8\u6027\u80fd\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u6ce8\u89e3\u6765\u7b80\u5316\u5f00\u53d1\u8fc7\u7a0b\u3002\u5176\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u300c@RequestMapping\u300d\u6ce8\u89e3\u6765\u5b9a\u4e49\u63a5\u53e3\uff0c\u5e76\u901a\u8fc7\u300c@Api\u300d\u6ce8\u89e3\u6765\u751f\u6210\u63a5\u53e3\u6587\u6863\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u9879\u76ee\u4e2d\u5f15\u5165Hyperf\u6846\u67b6\u4ee5\u53ca\u76f8\u5e94\u7684\u4f9d\u8d56\u3002\u5728composer.json\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>{\n    \"require\": {\n        \"hyperf\/http-server\": \"^2.0\",\n        \"phpstan\/phpstan\": \"^0.9.0\",\n        \"phpstan\/phpstan-strict-rules\": \"^0.9.0\",\n        \"symfony\/console\": \"^5.0\"\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\u6267\u884ccomposer update\u547d\u4ee4\u6765\u5b89\u88c5\u4f9d\u8d56\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\u6765\u5b9a\u4e49\u63a5\u53e3\u3002\u5728app\/Controller\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aIndexController.php\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php declare(strict_types=1);\n\nnamespace AppController;\n\nuse HyperfHttpServerAnnotationController;\nuse HyperfHttpServerAnnotationRequestMapping;\nuse HyperfDiAnnotationInject;\nuse HyperfContractStdoutLoggerInterface;\nuse AppServiceHelloService;\n\n\/**\n * @Controller()\n * @Api(name=\"Hello\u63a5\u53e3\")\n *\/\nclass IndexController\n{\n    \/**\n     * @Inject()\n     * @var HelloService\n     *\/\n    private $helloService;\n\n    \/**\n     * @RequestMapping(path=\"\/hello\/{name}\", methods=\"get\")\n     * @param string $name\n     *\/\n    public function hello(string $name)\n    {\n        return $this-&gt;helloService-&amp;gt;sayHello($name);\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2aIndexController\u63a7\u5236\u5668\uff0c\u5e76\u5728hello\u65b9\u6cd5\u4e0a\u4f7f\u7528\u4e86@RequestMapping\u6ce8\u89e3\u6765\u5b9a\u4e49\u63a5\u53e3\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u4f7f\u7528\u4e86@Api\u6ce8\u89e3\u6765\u751f\u6210\u63a5\u53e3\u6587\u6863\uff0c\u5e76\u4f7f\u7528\u4e86@Inject\u6ce8\u89e3\u6765\u6ce8\u5165HelloService\u670d\u52a1\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Hyperf\u6846\u67b6\u7684\u81ea\u5b9a\u4e49\u547d\u4ee4\u6765\u751f\u6210\u63a5\u53e3\u6587\u6863\u3002\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2adoc\u76ee\u5f55\uff0c\u5e76\u5728\u91cc\u9762\u521b\u5efa\u4e00\u4e2agenerate.php\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php declare(strict_types=1);\n\nuse HyperfCommandCommand as HyperfCommand;\nuse HyperfCommandAnnotationCommand;\nuse PsrContainerContainerInterface;\nuse HyperfApiDocCommandGenerateApiDocCommand;\n\n\/**\n * @Command\n *\/\nclass DocCommand extends HyperfCommand\n{\n    \/**\n     * @var ContainerInterface\n     *\/\n    protected $container;\n\n    public function __construct(ContainerInterface $container)\n    {\n        $this-&gt;container = $container;\n\n        parent::__construct('doc:generate');\n    }\n\n    public function configure()\n    {\n        parent::configure();\n        $this-&amp;gt;setDescription('Generate api doc for the project');\n    }\n\n    public function handle()\n    {\n        $command = new ApiDocCommand($this-&amp;gt;container);\n        $command-&amp;gt;setOutput($this-&amp;gt;output);\n        $command-&amp;gt;setInput($this-&amp;gt;input);\n        return $command-&amp;gt;handle();\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684DocCommand\u547d\u4ee4\uff0c\u5e76\u5728handle\u65b9\u6cd5\u4e2d\u8c03\u7528\u4e86Hyperf\u6846\u67b6\u63d0\u4f9b\u7684ApiDocCommand\u547d\u4ee4\u6765\u751f\u6210\u63a5\u53e3\u6587\u6863\u3002<\/p>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u5728\u7ec8\u7aef\u4e2d\u6267\u884cphp doc\/generate.php\u547d\u4ee4\u6765\u751f\u6210\u63a5\u53e3\u6587\u6863\u3002\u6210\u529f\u6267\u884c\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u7684public\u76ee\u5f55\u4e2d\u627e\u5230\u751f\u6210\u7684\u63a5\u53e3\u6587\u6863\u3002<\/p>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u6210\u529f\u5730\u4f7f\u7528Hyperf\u6846\u67b6\u751f\u6210\u4e86\u63a5\u53e3\u6587\u6863\u3002\u901a\u8fc7\u6ce8\u89e3\u5b9a\u4e49\u63a5\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u52a0\u7b80\u6d01\u5730\u7f16\u5199\u6587\u6863\uff0c\u51cf\u5c11\u4e86\u624b\u52a8\u7f16\u5199\u6587\u6863\u7684\u5de5\u4f5c\u91cf\u3002\u540c\u65f6\uff0cHyperf\u6846\u67b6\u63d0\u4f9b\u7684\u81ea\u5b9a\u4e49\u547d\u4ee4\u4e5f\u4f7f\u5f97\u6587\u6863\u7684\u751f\u6210\u8fc7\u7a0b\u66f4\u52a0\u4fbf\u6377\u3002<\/p>\n<p>\u7efc\u4e0a\u6240\u8ff0\uff0c\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u63a5\u53e3\u6587\u6863\u751f\u6210\u4e0d\u4ec5\u63d0\u9ad8\u4e86\u5f00\u53d1\u6548\u7387\uff0c\u8fd8\u80fd\u4fdd\u8bc1\u6587\u6863\u7684\u51c6\u786e\u6027\u548c\u4e00\u81f4\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u5927\u5bb6\u5728\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u63a5\u53e3\u6587\u6863\u751f\u6210\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee3\u7801\u793a\u4f8b\uff1ahttps:\/\/github.com\/xxx\/xxx<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u63a5\u53e3\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\u884c\u63a5\u53e3\u6587\u6863\u751f\u6210 \u968f\u7740Web\u5e94\u7528\u7a0b\u5e8f\u7684\u5feb\u901f\u53d1\u5c55\uff0c\u63a5\u53e3\u6587\u6863\u7684\u7f16\u5199\u548c\u7ef4\u62a4\u53d8\u5f97\u8d8a\u6765\u8d8a\u91cd\u8981\u3002\u63a5\u53e3\u6587\u6863\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u4e86\u89e3\u548c\u4f7f\u7528API\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u534f\u540c\u5f00\u53d1\u80fd\u529b\u3002\u5728\u65e5\u5e38\u7684\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u624b\u52a8\u7f16\u5199\u548c\u66f4\u65b0\u63a5\u53e3\u6587\u6863\uff0c\u8fd9\u662f\u4e00\u9879\u76f8\u5bf9\u7e41\u7410\u4e14\u5bb9\u6613\u51fa\u9519\u7684\u5de5\u4f5c\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Hyperf\u6846\u67b6\u7684\u81ea\u52a8\u5de5\u5177\uff0c\u901a\u8fc7\u6ce8\u89e3\u751f\u6210\u63a5\u53e3\u6587\u6863\u3002 Hyperf\u6846\u67b6\u662f\u57fa\u4e8eSwoole\u548cHyperf\u7ec4\u4ef6\u7684\u9ad8\u6027\u80fd\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u6ce8\u89e3\u6765\u7b80\u5316\u5f00\u53d1\u8fc7\u7a0b\u3002\u5176\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u300c@RequestMapping\u300d\u6ce8\u89e3\u6765\u5b9a\u4e49\u63a5\u53e3\uff0c\u5e76\u901a\u8fc7\u300c@Api\u300d\u6ce8\u89e3\u6765\u751f\u6210\u63a5\u53e3\u6587\u6863\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u9879\u76ee\u4e2d\u5f15\u5165Hyperf\u6846\u67b6\u4ee5\u53ca\u76f8\u5e94\u7684\u4f9d\u8d56\u3002\u5728composer.json\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a { &#8220;require&#8221;: { &#8220;hyperf\/http-server&#8221;: &#8220;^2.0&#8221;, &#8220;phpstan\/phpstan&#8221;: &#8220;^0.9.0&#8221;, &#8220;phpstan\/phpstan-strict-rules&#8221;: &#8220;^0.9.0&#8221;, &#8220;symfony\/console&#8221;: &#8220;^5.0&#8243; } } \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u6267\u884ccomposer update\u547d\u4ee4\u6765\u5b89\u88c5\u4f9d\u8d56\u3002 \u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u63a7\u5236\u5668\u6765\u5b9a\u4e49\u63a5\u53e3\u3002\u5728app\/Controller\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2aIndexController.php\u6587\u4ef6\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php declare(strict_types=1); namespace AppController; use HyperfHttpServerAnnotationController; use HyperfHttpServerAnnotationRequestMapping; use HyperfDiAnnotationInject; use HyperfContractStdoutLoggerInterface; use AppServiceHelloService; \/** * @Controller() * @Api(name=&#8221;Hello\u63a5\u53e3&#8221;) *\/ class IndexController { \/** * @Inject() * @var HelloService *\/ private $helloService; \/** * [&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-24772","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24772","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=24772"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24772\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}