{"id":24827,"date":"2024-11-21T17:38:22","date_gmt":"2024-11-21T09:38:22","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24827\/"},"modified":"2024-11-21T17:38:22","modified_gmt":"2024-11-21T09:38:22","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8laravel%e4%b8%ad%e4%bd%bf%e7%94%a8%e4%b8%ad%e9%97%b4%e4%bb%b6%e8%bf%9b%e8%a1%8c%e9%94%99%e8%af%af%e5%a4%84%e7%90%86","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24827\/","title":{"rendered":"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u9519\u8bef\u5904\u7406"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169890319878848.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u9519\u8bef\u5904\u7406\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u9519\u8bef\u5904\u7406\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u9519\u8bef\u5904\u7406<\/p>\n<p>\u5f15\u8a00\uff1a<br \/>Laravel\u662f\u4e00\u4e2a\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u5177\u6709\u5f3a\u5927\u7684\u9519\u8bef\u5904\u7406\u7cfb\u7edf\u3002\u5176\u4e2d\uff0c\u4e2d\u95f4\u4ef6\u662fLaravel\u7684\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u5728\u8bf7\u6c42\u8fdb\u5165\u8def\u7531\u5904\u7406\u4e4b\u524d\u6216\u4e4b\u540e\u6267\u884c\u4e00\u7cfb\u5217\u7684\u903b\u8f91\uff0c\u4ece\u800c\u5bf9\u9519\u8bef\u8fdb\u884c\u5904\u7406\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u521b\u5efa\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6<br \/>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u4e2d\u95f4\u4ef6\u6765\u5904\u7406\u9519\u8bef\u3002\u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u4e2d\u95f4\u4ef6\uff1a<br \/>php artisan make:middleware HandleErrors<\/p>\n<p>\u8be5\u547d\u4ee4\u5c06\u5728app\/Http\/Middleware\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aHandleErrors\u7684\u4e2d\u95f4\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728\u8be5\u4e2d\u95f4\u4ef6\u7684handle\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u6211\u4eec\u7684\u9519\u8bef\u5904\u7406\u903b\u8f91\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php namespace AppHttpMiddleware;\n\nuse Closure;\nuse Exception;\nuse IlluminateHttpRequest;\n\nclass HandleErrors\n{\n    public function handle(Request $request, Closure $next)\n    {\n        try {\n            return $next($request);\n        } catch (Exception $exception) {\n            \/\/ \u5728\u8fd9\u91cc\u5904\u7406\u9519\u8bef\n        }\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2atry-catch\u5757\u6765\u6355\u83b7\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u3002\u4e00\u65e6\u6355\u83b7\u5230\u5f02\u5e38\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728catch\u5757\u4e2d\u8fdb\u884c\u76f8\u5e94\u7684\u9519\u8bef\u5904\u7406\u64cd\u4f5c\u3002<\/p>\n<p>\u4e8c\u3001\u6ce8\u518c\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6<br \/>\u5728Laravel\u4e2d\uff0c\u9700\u8981\u5c06\u4e2d\u95f4\u4ef6\u6ce8\u518c\u5230\u5e94\u7528\u7a0b\u5e8f\u7684\u5168\u5c40\u4e2d\u95f4\u4ef6\u5806\u6808\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b8c\u6210\uff1a<\/p>\n<ol>\n<li>\u6253\u5f00app\/Http\/Kernel.php\u6587\u4ef6\u3002<\/li>\n<li>\u5728$middleware\u6570\u7ec4\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u884c\u4ee5\u6ce8\u518c\u4e2d\u95f4\u4ef6\uff1a<br \/>&#8216;error.handler&#8217; =&gt; AppHttpMiddlewareHandleErrors::class,<\/li>\n<\/ol>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u7684\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u5df2\u6210\u529f\u6ce8\u518c\u3002<\/p>\n<p>\u4e09\u3001\u4f7f\u7528\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6<br \/>\u8981\u5c06\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u5e94\u7528\u4e8e\u7279\u5b9a\u7684\u8def\u7531\u6216\u8def\u7531\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528Laravel\u7684route\u65b9\u6cd5\u6216group\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre>Route::group(['middleware' =&amp;gt; 'error.handler'], function () {\n    \/\/ \u5728\u8fd9\u91cc\u5b9a\u4e49\u9700\u8981\u5e94\u7528\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u7684\u8def\u7531\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86group\u65b9\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u7684\u8def\u7531\u7ec4\u3002\u5728\u8be5\u7ec4\u5185\u5b9a\u4e49\u7684\u8def\u7531\u5c06\u81ea\u52a8\u5e94\u7528\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u3002<\/p>\n<p>\u56db\u3001\u9519\u8bef\u5904\u7406\u793a\u4f8b<br \/>\u73b0\u5728\uff0c\u6211\u4eec\u5df2\u7ecf\u8bbe\u7f6e\u597d\u4e86\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u5c06\u5c55\u793a\u5982\u4f55\u5728\u4e2d\u95f4\u4ef6\u4e2d\u5904\u7406\u9519\u8bef\u3002<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3aUserController\u7684\u63a7\u5236\u5668\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2acreate\u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u7528\u6237\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>namespace AppHttpControllers;\n\nuse IlluminateHttpRequest;\n\nclass UserController extends Controller\n{\n    public function create(Request $request)\n    {\n        \/\/ \u521b\u5efa\u7528\u6237\u7684\u903b\u8f91\n\n        throw new Exception('\u521b\u5efa\u7528\u6237\u5931\u8d25');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u6545\u610f\u629b\u51fa\u4e86\u4e00\u4e2a\u5f02\u5e38\uff0c\u4ee5\u6a21\u62df\u9519\u8bef\u7684\u53d1\u751f\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u5728\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u4e2d\u6355\u83b7\u8be5\u5f02\u5e38\u5e76\u8fdb\u884c\u76f8\u5e94\u7684\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>namespace AppHttpMiddleware;\n\nuse Closure;\nuse Exception;\nuse IlluminateHttpRequest;\nuse IlluminateSupportFacadesLog;\n\nclass HandleErrors\n{\n    public function handle(Request $request, Closure $next)\n    {\n        try {\n            return $next($request);\n        } catch (Exception $exception) {\n            Log::error($exception-&amp;gt;getMessage());\n            return response()-&amp;gt;json(['error' =&amp;gt; '\u53d1\u751f\u4e86\u4e00\u4e2a\u9519\u8bef\uff0c\u8bf7\u7a0d\u540e\u518d\u8bd5'], 500);\n        }\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Log\u7c7b\u5c06\u9519\u8bef\u4fe1\u606f\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\uff0c\u5e76\u8fd4\u56de\u4e86\u4e00\u4e2a\u5305\u542b\u9519\u8bef\u4fe1\u606f\u7684JSON\u54cd\u5e94\u3002<\/p>\n<p>\u603b\u7ed3\uff1a<br \/>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u6210\u529f\u5730\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u4e86\u9519\u8bef\u5904\u7406\u3002\u9996\u5148\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u81ea\u5b9a\u4e49\u4e2d\u95f4\u4ef6\u6765\u5904\u7406\u9519\u8bef\uff0c\u5e76\u5c06\u5176\u6ce8\u518c\u5230\u4e2d\u95f4\u4ef6\u5806\u6808\u4e2d\u3002\u7136\u540e\uff0c\u6211\u4eec\u5e94\u7528\u4e86\u8be5\u4e2d\u95f4\u4ef6\u5230\u7279\u5b9a\u7684\u8def\u7531\u6216\u8def\u7531\u7ec4\uff0c\u5e76\u8fdb\u884c\u4e86\u9519\u8bef\u5904\u7406\u7684\u5177\u4f53\u903b\u8f91\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u7684\u903b\u8f91\u548c\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u5bf9\u5927\u5bb6\u4e86\u89e3\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u9519\u8bef\u5904\u7406\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u9519\u8bef\u5904\u7406\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\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u9519\u8bef\u5904\u7406 \u5f15\u8a00\uff1aLaravel\u662f\u4e00\u4e2a\u6d41\u884c\u7684PHP\u6846\u67b6\uff0c\u5177\u6709\u5f3a\u5927\u7684\u9519\u8bef\u5904\u7406\u7cfb\u7edf\u3002\u5176\u4e2d\uff0c\u4e2d\u95f4\u4ef6\u662fLaravel\u7684\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u5728\u8bf7\u6c42\u8fdb\u5165\u8def\u7531\u5904\u7406\u4e4b\u524d\u6216\u4e4b\u540e\u6267\u884c\u4e00\u7cfb\u5217\u7684\u903b\u8f91\uff0c\u4ece\u800c\u5bf9\u9519\u8bef\u8fdb\u884c\u5904\u7406\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Laravel\u4e2d\u4f7f\u7528\u4e2d\u95f4\u4ef6\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u521b\u5efa\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u4e2d\u95f4\u4ef6\u6765\u5904\u7406\u9519\u8bef\u3002\u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u4e2d\u95f4\u4ef6\uff1aphp artisan make:middleware HandleErrors \u8be5\u547d\u4ee4\u5c06\u5728app\/Http\/Middleware\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aHandleErrors\u7684\u4e2d\u95f4\u4ef6\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728\u8be5\u4e2d\u95f4\u4ef6\u7684handle\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u6211\u4eec\u7684\u9519\u8bef\u5904\u7406\u903b\u8f91\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a &lt;?php namespace AppHttpMiddleware; use Closure; use Exception; use IlluminateHttpRequest; class HandleErrors { public function handle(Request $request, Closure $next) { try { return $next($request); } catch (Exception $exception) { \/\/ \u5728\u8fd9\u91cc\u5904\u7406\u9519\u8bef } } } \u767b\u5f55\u540e\u590d\u5236 \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2atry-catch\u5757\u6765\u6355\u83b7\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u3002\u4e00\u65e6\u6355\u83b7\u5230\u5f02\u5e38\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728catch\u5757\u4e2d\u8fdb\u884c\u76f8\u5e94\u7684\u9519\u8bef\u5904\u7406\u64cd\u4f5c\u3002 \u4e8c\u3001\u6ce8\u518c\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u5728Laravel\u4e2d\uff0c\u9700\u8981\u5c06\u4e2d\u95f4\u4ef6\u6ce8\u518c\u5230\u5e94\u7528\u7a0b\u5e8f\u7684\u5168\u5c40\u4e2d\u95f4\u4ef6\u5806\u6808\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u5b8c\u6210\uff1a \u6253\u5f00app\/Http\/Kernel.php\u6587\u4ef6\u3002 \u5728$middleware\u6570\u7ec4\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\u884c\u4ee5\u6ce8\u518c\u4e2d\u95f4\u4ef6\uff1a&#8216;error.handler&#8217; =&gt; AppHttpMiddlewareHandleErrors::class, \u73b0\u5728\uff0c\u6211\u4eec\u7684\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u5df2\u6210\u529f\u6ce8\u518c\u3002 \u4e09\u3001\u4f7f\u7528\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u8981\u5c06\u9519\u8bef\u5904\u7406\u4e2d\u95f4\u4ef6\u5e94\u7528\u4e8e\u7279\u5b9a\u7684\u8def\u7531\u6216\u8def\u7531\u7ec4\uff0c\u53ef\u4ee5\u4f7f\u7528Laravel\u7684route\u65b9\u6cd5\u6216group\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a Route::group([&#8216;middleware&#8217; =&amp;gt; &#8216;error.handler&#8217;], function () { [&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-24827","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24827","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=24827"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24827\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}