{"id":24757,"date":"2024-11-21T16:57:53","date_gmt":"2024-11-21T08:57:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24757\/"},"modified":"2024-11-21T16:57:53","modified_gmt":"2024-11-21T08:57:53","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%e4%ba%8c%e7%bb%b4%e7%a0%81%e7%94%9f%e6%88%90","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24757\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u4e8c\u7ef4\u7801\u751f\u6210"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169810653734433.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u4e8c\u7ef4\u7801\u751f\u6210\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u4e8c\u7ef4\u7801\u751f\u6210\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u4e8c\u7ef4\u7801\u751f\u6210<\/p>\n<p>\u5f15\u8a00\uff1a<\/p>\n<p>\u968f\u7740\u4e8c\u7ef4\u7801\u7684\u5e7f\u6cdb\u5e94\u7528\uff0c\u4e8c\u7ef4\u7801\u751f\u6210\u7684\u9700\u6c42\u4e5f\u8d8a\u6765\u8d8a\u591a\u3002Hyperf\u6846\u67b6\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684PHP\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u5f88\u591a\u65b9\u4fbf\u5feb\u6377\u7684\u6269\u5c55\u80fd\u529b\uff0c\u5305\u62ec\u4e8c\u7ef4\u7801\u751f\u6210\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u4e8c\u7ef4\u7801\u751f\u6210\uff0c\u5e76\u9644\u4e0a\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u5b89\u88c5\u4f9d\u8d56<\/p>\n<p>\u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u51e0\u4e2a\u4f9d\u8d56\u5305\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528Composer\u5b89\u88c5endroid\/qr-code\u5305\uff1a<\/li>\n<\/ol>\n<pre>composer require endroid\/qr-code<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5728config\/autoload\/annotations.php\u4e2d\u6dfb\u52a0\u5bf9\u4e8eHyperf\u7684\u6ce8\u89e3\u652f\u6301\uff1a<\/li>\n<\/ol>\n<pre>&lt;?php declare(strict_types=1);\n\nuse HyperfDiAnnotationScan;\n\nreturn [\n    'scan' =&gt; [\n        Scan::class =&amp;gt; [\n            'paths' =&amp;gt; [\n                BASE_PATH . '\/app',\n            ],\n            'ignore_annotations' =&amp;gt; [\n            ],\n            'enable_scan_cache' =&amp;gt; env('ENABLE_ANNOTATION_CACHE', true),\n            'cache_key' =&amp;gt; 'annotations',\n            'exclude' =&amp;gt; [],\n            'proxy' =&amp;gt; [\n                'auto_generate' =&amp;gt; true,\n                'dir' =&amp;gt; BASE_PATH . '\/runtime\/container\/proxy',\n                'namespace' =&amp;gt; 'AppProxy',\n                'overwrite' =&amp;gt; false,\n            ],\n        ],\n    ],\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e8c\u3001\u521b\u5efa\u63a7\u5236\u5668<\/p>\n<p>\u5728Hyperf\u6846\u67b6\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u63a7\u5236\u5668\u6765\u5904\u7406HTTP\u8bf7\u6c42\u3002\u4e0b\u9762\u6211\u4eec\u521b\u5efa\u4e00\u4e2aQrCodeController\uff0c\u7528\u4e8e\u751f\u6210\u4e8c\u7ef4\u7801\u3002<\/p>\n<pre>&lt;?php declare(strict_types=1);\n\nnamespace AppController;\n\nuse HyperfHttpServerAnnotationController;\nuse HyperfHttpServerAnnotationRequestMapping;\nuse HyperfHttpServerContractResponseInterface;\nuse EndroidQrCodeResponseQrCodeResponse;\nuse EndroidQrCodeQrCode;\n\n\/**\n * @Controller(prefix=\"\/qrcode\")\n *\/\nclass QrCodeController\n{\n    \/**\n     * @RequestMapping(path=\"\/generate\", methods=\"get\")\n     *\/\n    public function generate(ResponseInterface $response)\n    {\n        $qrCode = new QRCode('https:\/\/www.example.com');\n        \n        return $response-&gt;withAddedHeader('Content-Type', QrCodeResponse::class)-&amp;gt;withBody(new SwooleStream($qrCode-&amp;gt;writeString()));\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e09\u3001\u914d\u7f6e\u8def\u7531<\/p>\n<p>\u5728config\/routes.php\u4e2d\u6dfb\u52a0\u5b9a\u4e49\u7684\u8def\u7531\u4fe1\u606f\u3002<\/p>\n<pre>&lt;?php declare(strict_types=1);\n\nuse HyperfHttpServerRouterRouter;\n\nRouter::get('\/qrcode\/generate', 'AppControllerQrCodeController@generate');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u56db\u3001\u6d4b\u8bd5\u751f\u6210\u4e8c\u7ef4\u7801<\/p>\n<p>\u542f\u52a8Hyperf\u6846\u67b6\uff0c\u5e76\u8bbf\u95eehttp:\/\/localhost:9501\/qrcode\/generate\uff0c\u5373\u53ef\u751f\u6210\u4e00\u4e2a\u5305\u542bhttps:\/\/www.example.com\u94fe\u63a5\u7684\u4e8c\u7ef4\u7801\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u4e8c\u7ef4\u7801\u751f\u6210\u3002\u901a\u8fc7\u5b89\u88c5\u4f9d\u8d56\u5305\uff0c\u521b\u5efa\u63a7\u5236\u5668\u548c\u914d\u7f6e\u8def\u7531\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Hyperf\u6846\u67b6\u4e2d\u751f\u6210\u4e8c\u7ef4\u7801\u3002\u5e0c\u671b\u80fd\u5bf9\u5927\u5bb6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u4e8c\u7ef4\u7801\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\u4e8c\u7ef4\u7801\u751f\u6210 \u5f15\u8a00\uff1a \u968f\u7740\u4e8c\u7ef4\u7801\u7684\u5e7f\u6cdb\u5e94\u7528\uff0c\u4e8c\u7ef4\u7801\u751f\u6210\u7684\u9700\u6c42\u4e5f\u8d8a\u6765\u8d8a\u591a\u3002Hyperf\u6846\u67b6\u4f5c\u4e3a\u4e00\u6b3e\u9ad8\u6027\u80fd\u7684PHP\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u5f88\u591a\u65b9\u4fbf\u5feb\u6377\u7684\u6269\u5c55\u80fd\u529b\uff0c\u5305\u62ec\u4e8c\u7ef4\u7801\u751f\u6210\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u4e8c\u7ef4\u7801\u751f\u6210\uff0c\u5e76\u9644\u4e0a\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u5b89\u88c5\u4f9d\u8d56 \u5728\u5f00\u59cb\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5b89\u88c5\u51e0\u4e2a\u4f9d\u8d56\u5305\u3002 \u4f7f\u7528Composer\u5b89\u88c5endroid\/qr-code\u5305\uff1a composer require endroid\/qr-code \u767b\u5f55\u540e\u590d\u5236 \u5728config\/autoload\/annotations.php\u4e2d\u6dfb\u52a0\u5bf9\u4e8eHyperf\u7684\u6ce8\u89e3\u652f\u6301\uff1a &lt;?php declare(strict_types=1); use HyperfDiAnnotationScan; return [ &#8216;scan&#8217; =&gt; [ Scan::class =&amp;gt; [ &#8216;paths&#8217; =&amp;gt; [ BASE_PATH . &#8216;\/app&#8217;, ], &#8216;ignore_annotations&#8217; =&amp;gt; [ ], &#8216;enable_scan_cache&#8217; =&amp;gt; env(&#8216;ENABLE_ANNOTATION_CACHE&#8217;, true), &#8216;cache_key&#8217; =&amp;gt; &#8216;annotations&#8217;, &#8216;exclude&#8217; =&amp;gt; [], &#8216;proxy&#8217; =&amp;gt; [ &#8216;auto_generate&#8217; =&amp;gt; true, &#8216;dir&#8217; =&amp;gt; BASE_PATH . &#8216;\/runtime\/container\/proxy&#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-24757","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24757","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=24757"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24757\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}