{"id":24765,"date":"2024-11-21T16:31:21","date_gmt":"2024-11-21T08:31:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24765\/"},"modified":"2024-11-21T16:31:21","modified_gmt":"2024-11-21T08:31:21","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8hyperf%e6%a1%86%e6%9e%b6%e5%bc%80%e5%8f%91web%e5%ba%94%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24765\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u5f00\u53d1Web\u5e94\u7528"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169819205880063.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u5f00\u53d1Web\u5e94\u7528\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u5f00\u53d1Web\u5e94\u7528\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u5f00\u53d1Web\u5e94\u7528<\/p>\n<p>\u5f15\u8a00<br \/>\u968f\u7740\u4e92\u8054\u7f51\u7684\u8fc5\u731b\u53d1\u5c55\uff0cWeb\u5e94\u7528\u7684\u9700\u6c42\u4e5f\u8d8a\u6765\u8d8a\u5927\u3002\u4e3a\u4e86\u6ee1\u8db3\u5f00\u53d1\u8005\u5bf9\u9ad8\u6027\u80fd\u3001\u9ad8\u6548\u7387\u7684\u9700\u6c42\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u6846\u67b6\u88ab\u5f00\u53d1\u51fa\u6765\u3002Hyperf\u6846\u67b6\u4f5c\u4e3a\u4e00\u4e2a\u57fa\u4e8eSwoole\u6269\u5c55\u5f00\u53d1\u7684\u9ad8\u6027\u80fd\u6846\u67b6\uff0c\u5df2\u7ecf\u53d7\u5230\u4e86\u5e7f\u5927\u5f00\u53d1\u8005\u7684\u5173\u6ce8\u548c\u559c\u7231\u3002\u672c\u6587\u5c06\u6307\u5bfc\u4f60\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u5f00\u53d1Web\u5e94\u7528\uff0c\u5e2e\u52a9\u4f60\u7406\u89e3Hyperf\u6846\u67b6\u7684\u6838\u5fc3\u6982\u5ff5\u548c\u4f7f\u7528\u65b9\u6cd5\u3002<\/p>\n<p>\u4e00\u3001\u5b89\u88c5Hyperf\u6846\u67b6<br \/>\u4f7f\u7528Composer\u5b89\u88c5Hyperf\u6846\u67b6\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\uff1a<\/p>\n<pre>composer create-project hyperf\/hyperf-skeleton<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5b89\u88c5\u6210\u529f\u540e\uff0c\u4f1a\u751f\u6210\u4e00\u4e2a\u540d\u4e3ahyperf-skeleton\u7684\u76ee\u5f55\u3002<\/p>\n<p>\u4e8c\u3001\u521b\u5efa\u63a7\u5236\u5668<br \/>\u5728Hyperf\u6846\u67b6\u4e2d\uff0c\u8def\u7531\u548c\u63a7\u5236\u5668\u662f\u7528\u6765\u5904\u7406\u8bf7\u6c42\u548c\u8fd4\u56de\u54cd\u5e94\u7684\u91cd\u8981\u7ec4\u4ef6\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u63a7\u5236\u5668\u6765\u5904\u7406\u8def\u7531\u8bf7\u6c42\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728hyperf-skeleton\u7684app\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3aController\u7684\u76ee\u5f55\uff0c\u7528\u6765\u5b58\u653e\u63a7\u5236\u5668\u6587\u4ef6\u3002<\/p>\n<p>\u7136\u540e\u5728Controller\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aIndexController\u7684PHP\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php declare(strict_types=1);\n\nnamespace AppController;\n\nuse HyperfHttpServerAnnotationAutoController;\nuse HyperfHttpServerContractRequestInterface;\nuse HyperfHttpServerContractResponseInterface;\n\n\/**\n * @AutoController(prefix=\"\/\")\n *\/\nclass IndexController\n{\n    public function index(RequestInterface $request, ResponseInterface $response)\n    {\n        return $response-&gt;raw('Hello Hyperf');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528\u4e86Hyperf\u6846\u67b6\u7684\u6ce8\u89e3@AutoController\u6765\u81ea\u52a8\u7ed1\u5b9a\u8def\u7531\uff0c\u4f7f\u7528\u4e86RequestInterface\u548cResponseInterface\u6765\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/p>\n<p>\u4e09\u3001\u914d\u7f6e\u8def\u7531<br \/>\u7ee7\u7eed\u5728app\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3aRoutes\u7684\u76ee\u5f55\uff0c\u7528\u6765\u5b58\u653e\u8def\u7531\u6587\u4ef6\u3002<\/p>\n<p>\u7136\u540e\u5728Routes\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aindex.php\u7684PHP\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php declare(strict_types=1);\n\nuse HyperfHttpServerRouterRouter;\n\nRouter::get('\/', 'AppControllerIndexController@index');<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u91cc\u4f7f\u7528\u4e86Hyperf\u6846\u67b6\u7684\u8def\u7531\u7ec4\u4ef6Router\u6765\u8bbe\u7f6e\u8def\u7531\uff0c\u5c06&#8217;\/&#8217;\u8def\u5f84\u6620\u5c04\u5230IndexController\u7684index\u65b9\u6cd5\u3002<\/p>\n<p>\u56db\u3001\u542f\u52a8\u670d\u52a1\u5668<br \/>\u5728hyperf-skeleton\u7684\u6839\u76ee\u5f55\u4e0b\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8Hyperf\u670d\u52a1\u5668\uff1a<\/p>\n<pre>php bin\/hyperf.php start<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f\u540e\uff0c\u4f60\u5c31\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95eehttp:\/\/localhost:9501\uff0c\u5e94\u8be5\u80fd\u770b\u5230Hello Hyperf\u7684\u8f93\u51fa\u3002<\/p>\n<p>\u603b\u7ed3<br \/>\u672c\u6587\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u5f00\u53d1Web\u5e94\u7528\u7684\u57fa\u672c\u6b65\u9aa4\uff0c\u5305\u62ec\u5b89\u88c5\u6846\u67b6\u3001\u521b\u5efa\u63a7\u5236\u5668\u3001\u914d\u7f6e\u8def\u7531\u548c\u542f\u52a8\u670d\u52a1\u5668\u3002\u5e0c\u671b\u901a\u8fc7\u8fd9\u7bc7\u6587\u7ae0\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u8fd0\u7528Hyperf\u6846\u67b6\u3002\u5728\u5b9e\u9645\u7684\u5f00\u53d1\u4e2d\uff0c\u4f60\u8fd8\u53ef\u4ee5\u6df1\u5165\u5b66\u4e60Hyperf\u6846\u67b6\u7684\u5176\u4ed6\u7279\u6027\u548c\u529f\u80fd\uff0c\u5f00\u53d1\u51fa\u66f4\u590d\u6742\u3001\u66f4\u9ad8\u6548\u7684Web\u5e94\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u5f00\u53d1Web\u5e94\u7528\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\u5f00\u53d1Web\u5e94\u7528 \u5f15\u8a00\u968f\u7740\u4e92\u8054\u7f51\u7684\u8fc5\u731b\u53d1\u5c55\uff0cWeb\u5e94\u7528\u7684\u9700\u6c42\u4e5f\u8d8a\u6765\u8d8a\u5927\u3002\u4e3a\u4e86\u6ee1\u8db3\u5f00\u53d1\u8005\u5bf9\u9ad8\u6027\u80fd\u3001\u9ad8\u6548\u7387\u7684\u9700\u6c42\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u6846\u67b6\u88ab\u5f00\u53d1\u51fa\u6765\u3002Hyperf\u6846\u67b6\u4f5c\u4e3a\u4e00\u4e2a\u57fa\u4e8eSwoole\u6269\u5c55\u5f00\u53d1\u7684\u9ad8\u6027\u80fd\u6846\u67b6\uff0c\u5df2\u7ecf\u53d7\u5230\u4e86\u5e7f\u5927\u5f00\u53d1\u8005\u7684\u5173\u6ce8\u548c\u559c\u7231\u3002\u672c\u6587\u5c06\u6307\u5bfc\u4f60\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u5f00\u53d1Web\u5e94\u7528\uff0c\u5e2e\u52a9\u4f60\u7406\u89e3Hyperf\u6846\u67b6\u7684\u6838\u5fc3\u6982\u5ff5\u548c\u4f7f\u7528\u65b9\u6cd5\u3002 \u4e00\u3001\u5b89\u88c5Hyperf\u6846\u67b6\u4f7f\u7528Composer\u5b89\u88c5Hyperf\u6846\u67b6\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u5373\u53ef\uff1a composer create-project hyperf\/hyperf-skeleton \u767b\u5f55\u540e\u590d\u5236 \u5b89\u88c5\u6210\u529f\u540e\uff0c\u4f1a\u751f\u6210\u4e00\u4e2a\u540d\u4e3ahyperf-skeleton\u7684\u76ee\u5f55\u3002 \u4e8c\u3001\u521b\u5efa\u63a7\u5236\u5668\u5728Hyperf\u6846\u67b6\u4e2d\uff0c\u8def\u7531\u548c\u63a7\u5236\u5668\u662f\u7528\u6765\u5904\u7406\u8bf7\u6c42\u548c\u8fd4\u56de\u54cd\u5e94\u7684\u91cd\u8981\u7ec4\u4ef6\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u63a7\u5236\u5668\u6765\u5904\u7406\u8def\u7531\u8bf7\u6c42\u3002 \u9996\u5148\uff0c\u5728hyperf-skeleton\u7684app\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3aController\u7684\u76ee\u5f55\uff0c\u7528\u6765\u5b58\u653e\u63a7\u5236\u5668\u6587\u4ef6\u3002 \u7136\u540e\u5728Controller\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aIndexController\u7684PHP\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a &lt;?php declare(strict_types=1); namespace AppController; use HyperfHttpServerAnnotationAutoController; use HyperfHttpServerContractRequestInterface; use HyperfHttpServerContractResponseInterface; \/** * @AutoController(prefix=&#8221;\/&#8221;) *\/ class IndexController { public function index(RequestInterface $request, ResponseInterface $response) { return $response-&gt;raw(&#8216;Hello Hyperf&#8217;); } } \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u91cc\u4f7f\u7528\u4e86Hyperf\u6846\u67b6\u7684\u6ce8\u89e3@AutoController\u6765\u81ea\u52a8\u7ed1\u5b9a\u8def\u7531\uff0c\u4f7f\u7528\u4e86RequestInterface\u548cResponseInterface\u6765\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\u3002 \u4e09\u3001\u914d\u7f6e\u8def\u7531\u7ee7\u7eed\u5728app\u76ee\u5f55\u4e0b\u65b0\u5efa\u4e00\u4e2a\u540d\u4e3aRoutes\u7684\u76ee\u5f55\uff0c\u7528\u6765\u5b58\u653e\u8def\u7531\u6587\u4ef6\u3002 \u7136\u540e\u5728Routes\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aindex.php\u7684PHP\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a &lt;?php declare(strict_types=1); use HyperfHttpServerRouterRouter; Router::get(&#8216;\/&#8217;, &#8216;AppControllerIndexController@index&#8217;); \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u91cc\u4f7f\u7528\u4e86Hyperf\u6846\u67b6\u7684\u8def\u7531\u7ec4\u4ef6Router\u6765\u8bbe\u7f6e\u8def\u7531\uff0c\u5c06&#8217;\/&#8217;\u8def\u5f84\u6620\u5c04\u5230IndexController\u7684index\u65b9\u6cd5\u3002 \u56db\u3001\u542f\u52a8\u670d\u52a1\u5668\u5728hyperf-skeleton\u7684\u6839\u76ee\u5f55\u4e0b\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u542f\u52a8Hyperf\u670d\u52a1\u5668\uff1a php bin\/hyperf.php [&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-24765","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24765","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=24765"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24765\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24765"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24765"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24765"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}