{"id":24726,"date":"2024-11-21T09:40:17","date_gmt":"2024-11-21T01:40:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24726\/"},"modified":"2024-11-21T09:40:17","modified_gmt":"2024-11-21T01:40: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%8c%e5%bc%82%e5%b8%b8%e6%8d%95%e8%8e%b7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24726\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5f02\u5e38\u6355\u83b7"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/465\/014\/169778810996452.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5f02\u5e38\u6355\u83b7\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5f02\u5e38\u6355\u83b7\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Hyper\u6846\u67b6\u8fdb\u884c\u5f02\u5e38\u6355\u83b7<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>\u5f02\u5e38\u662f\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u9047\u5230\u7684\u95ee\u9898\uff0c\u5408\u7406\u7684\u5f02\u5e38\u5904\u7406\u80fd\u591f\u63d0\u9ad8\u7cfb\u7edf\u7684\u53ef\u9760\u6027\u548c\u7a33\u5b9a\u6027\u3002Hyperf\u6846\u67b6\u662f\u4e00\u4e2a\u57fa\u4e8eSwoole\u6269\u5c55\u7684\u9ad8\u6027\u80fd\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5f02\u5e38\u6355\u83b7\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u5168\u5c40\u5f02\u5e38\u6355\u83b7<br \/>Hyperf\u6846\u67b6\u63d0\u4f9b\u4e86\u5168\u5c40\u5f02\u5e38\u6355\u83b7\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u5f02\u5e38\u53d1\u751f\u65f6\u4e2d\u65ad\u8bf7\u6c42\u7684\u6267\u884c\uff0c\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002\u901a\u8fc7\u5168\u5c40\u5f02\u5e38\u6355\u83b7\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5404\u79cd\u5f02\u5e38\u8fdb\u884c\u7edf\u4e00\u7684\u5904\u7406\uff0c\u5e76\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u9879\u76ee\u7684\u914d\u7f6e\u6587\u4ef6config\/autoload\/exceptions.php\u4e2d\u8fdb\u884c\u914d\u7f6e\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php return [\n    'handler' =&gt; [\n        'http' =&amp;gt; [\n            AppExceptionHandlerAppExceptionHandler::class,\n        ],\n    ],\n];<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u914d\u7f6e\u4e86\u4e00\u4e2ahttp\u7c7b\u578b\u7684\u5f02\u5e38\u5904\u7406\u7c7bAppExceptionHandlerAppExceptionHandler\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5b9a\u4e49\u8fd9\u4e2a\u7c7b\uff0c\u5e76\u5b9e\u73b0\u5f02\u5e38\u5904\u7406\u7684\u903b\u8f91\u3002\u4e00\u4e2a\u7b80\u5316\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace AppExceptionHandler;\n\nuse HyperfHttpServerExceptionHandlerHttpExceptionHandler;\nuse PsrHttpMessageResponseInterface;\nuse Throwable;\n\nclass AppExceptionHandler extends HttpExceptionHandler\n{\n    public function handle(Throwable $throwable, ResponseInterface $response)\n    {   \n        \/\/ \u5f02\u5e38\u5904\u7406\u903b\u8f91\n        \/\/ \u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u7ed9\u5ba2\u6237\u7aef\n        \n        return $response;\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7ee7\u627f\u4e86HttpExceptionHandler\u7c7b\uff0c\u5e76\u5b9e\u73b0\u4e86handle()\u65b9\u6cd5\uff0c\u5728\u8be5\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u5904\u7406\u6355\u83b7\u5230\u7684\u5f02\u5e38\u3002<\/p>\n<p>\u4e8c\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406<br \/>\u9664\u4e86\u5168\u5c40\u5f02\u5e38\u6355\u83b7\u5916\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u81ea\u5b9a\u4e49\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u6355\u83b7\u548c\u5904\u7406\u7279\u5b9a\u7684\u5f02\u5e38\u3002\u5728Hyperf\u6846\u67b6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627fAbstractExceptionHandler\u7c7b\uff0c\u5e76\u91cd\u5199handle()\u65b9\u6cd5\u6765\u5b9e\u73b0\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u3002\u4e0b\u9762\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u6765\u8bf4\u660e\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aAppExceptionHandler\u7684\u7c7b\uff0c\u5728\u8be5\u7c7b\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u5904\u7406\u903b\u8f91\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;?php namespace AppExceptionHandler;\n\nuse HyperfExceptionHandlerExceptionHandler;\nuse HyperfHttpMessageStreamSwooleStream;\nuse PsrHttpMessageResponseInterface;\nuse Throwable;\n\nclass AppExceptionHandler extends ExceptionHandler\n{\n    public function handle(Throwable $throwable, ResponseInterface $response)\n    {\n        \/\/ \u5f02\u5e38\u5904\u7406\u903b\u8f91\n        \/\/ \u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u7ed9\u5ba2\u6237\u7aef\n        \n        return $response-&gt;withStatus(500)\n            -&amp;gt;withAddedHeader('Content-Type', 'application\/json')\n            -&amp;gt;withBody(new SwooleStream(json_encode([\n                'code' =&amp;gt; $throwable-&amp;gt;getCode(),\n                'message' =&amp;gt; $throwable-&amp;gt;getMessage(),\n            ])));\n    }\n\n    public function isValid(Throwable $throwable): bool\n    {\n        return true; \/\/ \u6355\u83b7\u6240\u6709\u5f02\u5e38\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7ee7\u627f\u4e86ExceptionHandler\u7c7b\uff0c\u5e76\u5b9e\u73b0\u4e86handle()\u65b9\u6cd5\u548cisValid()\u65b9\u6cd5\u3002\u5728handle()\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u5904\u7406\u6355\u83b7\u5230\u7684\u5f02\u5e38\uff0c\u5e76\u5c06\u9519\u8bef\u4fe1\u606f\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\uff0c\u5728isValid()\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u51b3\u5b9a\u662f\u5426\u8981\u6355\u83b7\u8be5\u5f02\u5e38\u3002<\/p>\n<p>\u4e09\u3001\u5f02\u5e38\u5904\u7406\u987a\u5e8f<br \/>\u5728Hyperf\u6846\u67b6\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u7684\u987a\u5e8f\u662f\u4eceException\u5230Throwable\uff0c\u5373\u5f02\u5e38\u662f\u4ece\u4e0a\u5230\u4e0b\u4f9d\u6b21\u5904\u7406\u7684\u3002\u5728\u5df2\u7ecf\u6709\u4e86\u5168\u5c40\u5f02\u5e38\u6355\u83b7\u548c\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u7684\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u67d0\u4e2a\u5f02\u5e38\u540c\u65f6\u7b26\u5408\u591a\u4e2a\u5f02\u5e38\u5904\u7406\u5668\u7684\u5904\u7406\u6761\u4ef6\uff0c\u6846\u67b6\u4f1a\u6839\u636e\u5904\u7406\u5668\u7684\u4f18\u5148\u7ea7\u6765\u786e\u5b9a\u4f7f\u7528\u54ea\u4e2a\u5904\u7406\u5668\u3002<\/p>\n<p>\u5728\u914d\u7f6e\u6587\u4ef6config\/autoload\/exceptions.php\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u5668\u7684\u914d\u7f6e\u987a\u5e8f\u5373\u662f\u5904\u7406\u5668\u7684\u4f18\u5148\u7ea7\u987a\u5e8f\u3002\u914d\u7f6e\u540e\u9762\u7684\u5904\u7406\u5668\u4f1a\u4f18\u5148\u5904\u7406\u5f02\u5e38\u3002\u5982\u679c\u67d0\u4e2a\u5904\u7406\u5668\u6210\u529f\u5904\u7406\u4e86\u5f02\u5e38\uff0c\u540e\u7eed\u7684\u5904\u7406\u5668\u5c06\u4e0d\u518d\u5904\u7406\u3002<\/p>\n<p>\u5c0f\u7ed3\uff1a<br \/>\u5408\u7406\u7684\u5f02\u5e38\u5904\u7406\u662f\u5f00\u53d1\u9ad8\u8d28\u91cf\u7cfb\u7edf\u7684\u91cd\u8981\u4e00\u73af\uff0cHyperf\u6846\u67b6\u63d0\u4f9b\u4e86\u5168\u5c40\u5f02\u5e38\u6355\u83b7\u548c\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u7684\u673a\u5236\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u7075\u6d3b\u4e14\u9ad8\u6548\u7684\u5f02\u5e38\u5904\u7406\u3002\u672c\u6587\u8bb2\u89e3\u4e86\u5982\u4f55\u914d\u7f6e\u5168\u5c40\u5f02\u5e38\u6355\u83b7\u4ee5\u53ca\u7f16\u5199\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u903b\u8f91\uff0c\u5e76\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u8bfb\u8005\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5f02\u5e38\u6355\u83b7\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\u7528Hyper\u6846\u67b6\u8fdb\u884c\u5f02\u5e38\u6355\u83b7 \u5f15\u8a00\uff1a\u5f02\u5e38\u662f\u7f16\u7a0b\u8fc7\u7a0b\u4e2d\u7ecf\u5e38\u9047\u5230\u7684\u95ee\u9898\uff0c\u5408\u7406\u7684\u5f02\u5e38\u5904\u7406\u80fd\u591f\u63d0\u9ad8\u7cfb\u7edf\u7684\u53ef\u9760\u6027\u548c\u7a33\u5b9a\u6027\u3002Hyperf\u6846\u67b6\u662f\u4e00\u4e2a\u57fa\u4e8eSwoole\u6269\u5c55\u7684\u9ad8\u6027\u80fd\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5f02\u5e38\u5904\u7406\u673a\u5236\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5f02\u5e38\u6355\u83b7\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u5168\u5c40\u5f02\u5e38\u6355\u83b7Hyperf\u6846\u67b6\u63d0\u4f9b\u4e86\u5168\u5c40\u5f02\u5e38\u6355\u83b7\u7684\u529f\u80fd\uff0c\u53ef\u4ee5\u5728\u5f02\u5e38\u53d1\u751f\u65f6\u4e2d\u65ad\u8bf7\u6c42\u7684\u6267\u884c\uff0c\u6355\u83b7\u5e76\u5904\u7406\u5f02\u5e38\u3002\u901a\u8fc7\u5168\u5c40\u5f02\u5e38\u6355\u83b7\uff0c\u6211\u4eec\u53ef\u4ee5\u5bf9\u5404\u79cd\u5f02\u5e38\u8fdb\u884c\u7edf\u4e00\u7684\u5904\u7406\uff0c\u5e76\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u76f8\u5e94\u7684\u9519\u8bef\u4fe1\u606f\u3002 \u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u5728\u9879\u76ee\u7684\u914d\u7f6e\u6587\u4ef6config\/autoload\/exceptions.php\u4e2d\u8fdb\u884c\u914d\u7f6e\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php return [ &#8216;handler&#8217; =&gt; [ &#8216;http&#8217; =&amp;gt; [ AppExceptionHandlerAppExceptionHandler::class, ], ], ]; \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u914d\u7f6e\u4e86\u4e00\u4e2ahttp\u7c7b\u578b\u7684\u5f02\u5e38\u5904\u7406\u7c7bAppExceptionHandlerAppExceptionHandler\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5b9a\u4e49\u8fd9\u4e2a\u7c7b\uff0c\u5e76\u5b9e\u73b0\u5f02\u5e38\u5904\u7406\u7684\u903b\u8f91\u3002\u4e00\u4e2a\u7b80\u5316\u7684\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php namespace AppExceptionHandler; use HyperfHttpServerExceptionHandlerHttpExceptionHandler; use PsrHttpMessageResponseInterface; use Throwable; class AppExceptionHandler extends HttpExceptionHandler { public function handle(Throwable $throwable, ResponseInterface $response) { \/\/ \u5f02\u5e38\u5904\u7406\u903b\u8f91 \/\/ \u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u7ed9\u5ba2\u6237\u7aef return $response; } } \u767b\u5f55\u540e\u590d\u5236 \u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u7ee7\u627f\u4e86HttpExceptionHandler\u7c7b\uff0c\u5e76\u5b9e\u73b0\u4e86handle()\u65b9\u6cd5\uff0c\u5728\u8be5\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u5904\u7406\u6355\u83b7\u5230\u7684\u5f02\u5e38\u3002 \u4e8c\u3001\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u9664\u4e86\u5168\u5c40\u5f02\u5e38\u6355\u83b7\u5916\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u81ea\u5b9a\u4e49\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u6355\u83b7\u548c\u5904\u7406\u7279\u5b9a\u7684\u5f02\u5e38\u3002\u5728Hyperf\u6846\u67b6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7ee7\u627fAbstractExceptionHandler\u7c7b\uff0c\u5e76\u91cd\u5199handle()\u65b9\u6cd5\u6765\u5b9e\u73b0\u81ea\u5b9a\u4e49\u5f02\u5e38\u5904\u7406\u3002\u4e0b\u9762\u4ee5\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u6765\u8bf4\u660e\u3002 \u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aAppExceptionHandler\u7684\u7c7b\uff0c\u5728\u8be5\u7c7b\u4e2d\u5b9e\u73b0\u81ea\u5b9a\u4e49\u7684\u5f02\u5e38\u5904\u7406\u903b\u8f91\u3002\u4ee3\u7801\u5982\u4e0b\uff1a &lt;?php namespace AppExceptionHandler; [&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-24726","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24726","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=24726"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24726\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24726"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24726"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24726"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}