{"id":807,"date":"2025-06-12T00:10:53","date_gmt":"2025-06-11T16:10:53","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/laravel\/807.html"},"modified":"2025-06-12T00:10:53","modified_gmt":"2025-06-11T16:10:53","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-laravel-%e4%b8%ad%e5%a4%84%e7%90%86%e5%bc%82%e5%b8%b8%ef%bc%9a%e5%ae%9e%e7%94%a8%e6%8c%87%e5%8d%97","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/807.html","title":{"rendered":"\u5982\u4f55\u5728 Laravel \u4e2d\u5904\u7406\u5f02\u5e38\uff1a\u5b9e\u7528\u6307\u5357"},"content":{"rendered":"<div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--1\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/dyA-1-400x300.jpg\" alt=\"Bug&#32534;&#35793;&#29422;\" class=\"wp-image-1842\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">Bug&#32534;&#35793;&#29422;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312; Laravel &#20013;&#22788;&#29702;&#24322;&#24120;&#26159;&#30830;&#20445;&#24212;&#29992;&#31243;&#24207;&#31283;&#23450;&#24615;&#21644;&#29992;&#25143;&#21451;&#22909;&#24615;&#30340;&#37325;&#35201;&#27493;&#39588;&#12290;&#20197;&#19979;&#26159;&#19968;&#20123;&#23454;&#29992;&#30340;&#25351;&#21335;&#21644;&#31034;&#20363;&#20195;&#30721;&#65292;&#24110;&#21161;&#20320;&#22312; Laravel &#24212;&#29992;&#31243;&#24207;&#20013;&#26377;&#25928;&#22320;&#22788;&#29702;&#24322;&#24120;&#12290;<\/p>\n<h3>1. &#24322;&#24120;&#22522;&#26412;&#27010;&#24565;<\/h3>\n<ul>\n<li><strong>Exception<\/strong>: &#25243;&#20986;&#30340;&#38169;&#35823;&#25110;&#24322;&#24120;&#12290;<\/li>\n<li><strong>Handler<\/strong>: &#22788;&#29702;&#24322;&#24120;&#30340;&#31867;&#65292;&#36890;&#24120;&#20301;&#20110; <code>app\/Exceptions<\/code> &#30446;&#24405;&#19979;&#12290;<\/li>\n<li><strong>Middleware<\/strong>: &#20013;&#38388;&#20214;&#29992;&#20110;&#25429;&#33719;&#21644;&#22788;&#29702;&#35831;&#27714;&#21644;&#21709;&#24212;&#12290;<\/li>\n<\/ul>\n<h3>2. &#21019;&#24314;&#24322;&#24120;&#22788;&#29702;&#22120;<\/h3>\n<p>&#39318;&#20808;&#65292;&#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#24322;&#24120;&#22788;&#29702;&#22120;&#31867;&#12290;&#20363;&#22914;&#65306;<\/p>\n<pre><code class=\"language-php\">\/\/ app\/Exceptions\/Handler.php\nnamespace AppExceptions;\n\nuse Exception;\nuse IlluminateFoundationExceptionsHandler as BaseHandler;\n\nclass Handler extends BaseHandler\n{\n    protected $dontReport = [\n        \/\/ &#19981;&#35201;&#25253;&#21578;&#30340;&#24322;&#24120;&#31867;&#22411;\n    ];\n\n    public function report(Exception $e)\n    {\n        \/\/ &#35843;&#29992;&#22522;&#31867;&#30340;&#26041;&#27861;&#36827;&#34892;&#24120;&#35268;&#30340;&#24322;&#24120;&#22788;&#29702;\n        parent::report($e);\n    }\n\n    public function render($request, Exception $e)\n    {\n        \/\/ &#33258;&#23450;&#20041;&#28210;&#26579;&#36923;&#36753;\n        return response()-&gt;json(['error' =&gt; $e-&gt;getMessage()], 500);\n    }\n}<\/code><\/pre>\n<h3>3. &#27880;&#20876;&#24322;&#24120;&#22788;&#29702;&#22120;<\/h3>\n<p>&#22312; <code>config\/app.php<\/code> &#25991;&#20214;&#20013;&#27880;&#20876;&#24322;&#24120;&#22788;&#29702;&#22120;&#65306;<\/p>\n<pre><code class=\"language-php\">\/\/ config\/app.php\nreturn [\n    'providers' =&gt; [\n        \/\/ &#20854;&#20182;&#26381;&#21153;&#25552;&#20379;&#32773;\n    ],\n\n    'aliases' =&gt; [\n        \/\/ &#20854;&#20182;&#21035;&#21517;\n    ],\n\n    'exception_handler' =&gt; AppExceptionsHandler::class,\n];<\/code><\/pre>\n<h3>4. &#25429;&#33719;&#29305;&#23450;&#24322;&#24120;<\/h3>\n<p>&#20320;&#21487;&#20197;&#36890;&#36807;&#37325;&#36733; <code>render<\/code> &#26041;&#27861;&#26469;&#25429;&#33719;&#29305;&#23450;&#31867;&#22411;&#30340;&#24322;&#24120;&#65292;&#24182;&#36820;&#22238;&#33258;&#23450;&#20041;&#30340; JSON &#21709;&#24212;&#12290;<\/p>\n<pre><code class=\"language-php\">public function render($request, Exception $e)\n{\n    if ($e instanceof MyCustomException) {\n        return response()-&gt;json(['message' =&gt; 'This is a custom error message'], 400);\n    }\n\n    return parent::render($request, $e);\n}<\/code><\/pre>\n<h3>5. &#20351;&#29992;&#20013;&#38388;&#20214;&#25429;&#33719;&#20840;&#23616;&#24322;&#24120;<\/h3>\n<p>&#20320;&#20063;&#21487;&#20197;&#20351;&#29992;&#20013;&#38388;&#20214;&#26469;&#25429;&#33719;&#20840;&#23616;&#24322;&#24120;&#12290;<\/p>\n<pre><code class=\"language-php\">\/\/ app\/Http\/Middleware\/GlobalExceptionHandler.php\nnamespace AppHttpMiddleware;\n\nuse Closure;\nuse Exception;\n\nclass GlobalExceptionHandler\n{\n    public function handle($request, Closure $next)\n    {\n        try {\n            return $next($request);\n        } catch (Exception $e) {\n            \/\/ &#22788;&#29702;&#24322;&#24120;&#24182;&#36820;&#22238;&#33258;&#23450;&#20041;&#21709;&#24212;\n            return response()-&gt;json(['error' =&gt; $e-&gt;getMessage()], 500);\n        }\n    }\n}<\/code><\/pre>\n<p>&#22312; <code>app\/Kernel.php<\/code> &#20013;&#27880;&#20876;&#20013;&#38388;&#20214;&#65306;<\/p>\n<pre><code class=\"language-php\">protected $middleware = [\n    \/\/ &#20854;&#20182;&#20013;&#38388;&#20214;\n    AppHttpMiddlewareGlobalExceptionHandler::class,\n];<\/code><\/pre>\n<h3>&#31034;&#20363;&#20195;&#30721;<\/h3>\n<p>&#20197;&#19979;&#26159;&#19968;&#20010;&#23436;&#25972;&#30340;&#31034;&#20363;&#65292;&#23637;&#31034;&#20102;&#22914;&#20309;&#22312; Laravel &#20013;&#22788;&#29702;&#24322;&#24120;&#65306;<\/p>\n<pre><code class=\"language-php\">\/\/ app\/Exceptions\/MyCustomException.php\nnamespace AppExceptions;\n\nuse Exception;\n\nclass MyCustomException extends Exception\n{\n    public function __construct($message = '', $code = 0, Throwable $previous = null)\n    {\n        parent::__construct($message, $code, $previous);\n    }\n}\n\n\/\/ app\/Exceptions\/Handler.php\nnamespace AppExceptions;\n\nuse Exception;\nuse IlluminateFoundationExceptionsHandler as BaseHandler;\n\nclass Handler extends BaseHandler\n{\n    protected $dontReport = [\n        MyCustomException::class,\n    ];\n\n    public function report(Exception $e)\n    {\n        parent::report($e);\n    }\n\n    public function render($request, Exception $e)\n    {\n        if ($e instanceof MyCustomException) {\n            return response()-&gt;json(['message' =&gt; 'This is a custom error message'], 400);\n        }\n\n        return parent::render($request, $e);\n    }\n}\n\n\/\/ app\/Http\/Middleware\/GlobalExceptionHandler.php\nnamespace AppHttpMiddleware;\n\nuse Closure;\nuse Exception;\n\nclass GlobalExceptionHandler\n{\n    public function handle($request, Closure $next)\n    {\n        try {\n            return $next($request);\n        } catch (Exception $e) {\n            \/\/ &#22788;&#29702;&#24322;&#24120;&#24182;&#36820;&#22238;&#33258;&#23450;&#20041;&#21709;&#24212;\n            return response()-&gt;json(['error' =&gt; $e-&gt;getMessage()], 500);\n        }\n    }\n}\n\n\/\/ routes\/web.php\nuse AppHttpControllersExampleController;\nuse AppExceptionsMyCustomException;\n\nRoute::get('\/example', [ExampleController::class, 'index'])-&gt;catch(MyCustomException::class);\n\n\/\/ app\/Http\/Controllers\/ExampleController.php\nnamespace AppHttpControllers;\n\nuse AppExceptionsMyCustomException;\nuse IlluminateHttpRequest;\n\nclass ExampleController\n{\n    public function index()\n    {\n        throw new MyCustomException('Something went wrong!');\n    }\n}<\/code><\/pre>\n<p>&#36890;&#36807;&#20197;&#19978;&#27493;&#39588;&#65292;&#20320;&#21487;&#20197;&#22312; Laravel &#20013;&#26377;&#25928;&#22320;&#22788;&#29702;&#24322;&#24120;&#65292;&#24182;&#20026;&#29992;&#25143;&#25552;&#20379;&#26356;&#22909;&#30340;&#29992;&#25143;&#20307;&#39564;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--2\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/zp-400x300.jpg\" alt=\"&#40657;&#26495;Bug&#35762;&#24072;\" class=\"wp-image-1849\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#40657;&#26495;Bug&#35762;&#24072;<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<body><h2>&#20171;&#32461;<\/h2><p>Laravel &#26159;&#19968;&#20010;&#22522;&#20110; PHP &#30340;&#24378;&#22823; MVC &#26694;&#26550;&#65292;&#25317;&#26377;&#24378;&#22823;&#30340;&#38169;&#35823;&#21644;&#24322;&#24120;&#22788;&#29702;&#31995;&#32479;&#12290;&#26377;&#25928;&#22320;&#22788;&#29702;&#24322;&#24120;&#23545;&#20110;&#26500;&#24314;&#21487;&#38752;&#30340;&#24212;&#29992;&#31243;&#24207;&#24182;&#20026;&#29992;&#25143;&#25552;&#20379;&#24841;&#24555;&#30340;&#29992;&#25143;&#20307;&#39564;&#33267;&#20851;&#37325;&#35201;&#12290;&#26412;&#25351;&#21335;&#23558;&#25945;&#20320;&#22914;&#20309;&#31995;&#32479;&#22320;&#31649;&#29702;&#24322;&#24120;&#65292;&#24182;&#23637;&#31034;&#22914;&#20309;&#36890;&#36807; Laravel &#26356;&#36731;&#26494;&#22320;&#22788;&#29702;&#22312;&#25191;&#34892;&#24212;&#29992;&#31243;&#24207;&#26102;&#20986;&#29616;&#30340;&#38169;&#35823;&#12290;<\/p><h2>&#22312;Laravel&#20013;&#29702;&#35299;&#24322;&#24120;&#65288;Exceptions&#65289;<\/h2><p>Laravel&#30340;&#24322;&#24120;&#22788;&#29702;&#38598;&#20013;&#22312;&#20854;&#20013;&#12290;<code>appExceptionsHandler.php<\/code>&#25991;&#20214;&#12290;&#23427;&#20351;&#29992;&#20102;&#19968;&#20010;&#21517;&#20026;&#30340;&#31867;&#12290;<code>Handler<\/code>&#35813;&#26041;&#27861;&#21253;&#21547;&#20004;&#20010;&#26041;&#27861;&#65306;<code>report()<\/code>and&#20063;&#26159;&#65292;&#25105;&#20204;&#32487;&#32493;&#21543;&#12290;<code>render()<\/code>&#36825;&#26159;&#20010;&#31616;&#35201;&#27010;&#36848;&#65306;<\/p><p> <code>render()<\/code>&#23427;&#20250;&#21709;&#24212;&#24322;&#24120;&#24182;&#20915;&#23450;&#24212;&#29992;&#31243;&#24207;&#24212;&#35813;&#21521;&#29992;&#25143;&#26174;&#31034;&#20160;&#20040;&#12290;<\/p><p> <code>report()<\/code>&#35813;&#26041;&#27861;&#35760;&#24405;&#24322;&#24120;&#12290;&#24744;&#21487;&#20197;&#21521;&#22806;&#37096;&#26381;&#21153;&#22914;Bugsnag&#25110;Sentry&#25253;&#21578;&#36825;&#20123;&#24322;&#24120;&#12290;<\/p><h2>&#22522;&#26412;&#30340;&#24322;&#24120;&#22788;&#29702;&#26426;&#21046;<\/h2><p>&#20197;&#19979;&#26159;&#21019;&#24314;&#33258;&#23450;&#20041;&#24322;&#24120;&#30340;&#31616;&#21333;&#31034;&#20363;&#65306;<\/p><pre><code>class MyCustomException extends Exception {}\n\n\/\/ Throwing the custom exception\nthrow new MyCustomException('Something went wrong');<\/code><\/pre><p>&#20026;&#20102;&#22788;&#29702;&#36825;&#20010;&#24322;&#24120;&#65292;&#38656;&#35201;&#37325;&#20889;&#23427;&#12290;<code>render()<\/code>&#26041;&#27861;&#65306;<\/p><pre><code>public function render($request, Exception $exception)\n{\n    if ($exception instanceof MyCustomException) {\n        return response()-&gt;view('errors.custom', [], 500);\n    }\n\n    return parent::render($request, $exception);\n}<\/code><\/pre><h2>&#39564;&#35777;&#24322;&#24120;<\/h2><p>Laravel &#25552;&#20379;&#20102;&#19968;&#20010;&#24110;&#21161;&#22788;&#29702;&#33258;&#21160;&#39564;&#35777;&#38169;&#35823;&#30340;&#29305;&#24615;&#12290;&#20351;&#29992;&#23427;&#12290;<code>validate<\/code>&#22312;&#24744;&#30340;&#25511;&#21046;&#22120;&#26041;&#27861;&#20013;&#28155;&#21152;&#36741;&#21161;&#26041;&#27861;&#20197;&#21033;&#29992;&#36825;&#19968;&#28857;&#65306;<\/p><pre><code>public function store(Request $request)\n{\n    $validatedData = $request-&gt;validate([\n        'title' =&gt; 'required|unique:posts|max:255',\n        'body' =&gt; 'required',\n    ]);\n\n    \/\/ The blog post is valid, keep going...\n}<\/code><\/pre><p>&#22914;&#26524;&#39564;&#35777;&#22833;&#36133;&#65292;&#37027;&#20040;&#19968;&#20010;<code>ValidationException<\/code>&#34987;Laravel&#30340;&#24322;&#24120;&#22788;&#29702;&#22120;&#25429;&#33719;&#24182;&#22788;&#29702;&#12290;&#29992;&#25143;&#20250;&#34987;&#37325;&#23450;&#21521;&#22238;&#20043;&#21069;&#30340;&#20301;&#32622;&#65292;&#21516;&#26102;&#33258;&#21160;&#38378;&#29616;&#39564;&#35777;&#38169;&#35823;&#21040;&#20250;&#35805;&#20013;&#12290;<\/p><h2>&#35748;&#35777;&#20363;&#22806;&#24773;&#20917;<\/h2><p>&#22312;&#35748;&#35777;&#36807;&#31243;&#20013;&#65292;&#22914;&#26524;&#29992;&#25143;&#23581;&#35797;&#35775;&#38382;&#19968;&#20010;&#38656;&#35201;&#35748;&#35777;&#30340;&#36335;&#30001;&#65292;&#37027;&#20040;&#20250;&#35302;&#21457;&#35748;&#35777;&#27969;&#31243;&#12290;<code>IlluminateAuthAuthenticationException<\/code>&#34987;&#25243;&#20986;&#12290;Laravel &#20250;&#37325;&#23450;&#21521;&#21040;&#30331;&#24405;&#39029;&#38754;&#65292;&#25110;&#32773;&#23545;&#20110;API&#35831;&#27714;&#65292;&#36820;&#22238;&#19968;&#20010;&#24102;&#26377;401&#29366;&#24577;&#30721;&#30340;JSON&#21709;&#24212;&#12290;&#23427;&#40664;&#35748;&#23601;&#37197;&#32622;&#22909;&#20102;&#65292;&#20294;&#20320;&#21487;&#20197;&#36890;&#36807;&#20462;&#25913;&#26469;&#23450;&#21046;&#36825;&#20010;&#34892;&#20026;&#12290;<code>Handler<\/code>&#23545;&#19981;&#36215;&#65292;&#25105;&#19981;&#22826;&#26126;&#30333;&#24744;&#30340;&#38382;&#39064;&#65292;&#35831;&#24744;&#37325;&#26032;&#25551;&#36848;&#19968;&#19979;&#12290;<\/p><pre><code>protected function unauthenticated($request, AuthenticationException $exception)\n{\n    return $request-&gt;expectsJson()\n        ? response()-&gt;json(['message' =&gt; 'Unauthenticated.'], 401)\n        : redirect()-&gt;guest(route('login'));\n}<\/code><\/pre><h2>&#25253;&#21578;&#21644;&#35760;&#24405;&#24322;&#24120;&#24773;&#20917;<\/h2><p>&#25253;&#21578;&#24037;&#20316;&#28041;&#21450;&#35760;&#24405;&#24322;&#24120;&#25110;&#23558;&#20854;&#21457;&#36865;&#21040;&#22806;&#37096;&#26381;&#21153;&#65292;&#20363;&#22914;Sentry&#12290;&#40664;&#35748;&#24773;&#20917;&#19979;&#65292;Laravel&#20250;&#35760;&#24405;&#24322;&#24120;&#65292;&#20294;&#24744;&#21487;&#20197;&#23545;&#20854;&#36827;&#34892;&#33258;&#23450;&#20041;&#65306;<\/p><pre><code>public function report(Exception $exception)\n{\n    if ($exception instanceof CustomException) {\n        \/\/ Send the exception to an external service\n    }\n\n    parent::report($exception);\n}<\/code><\/pre><p>&#24573;&#30053;&#26576;&#20123;&#20363;&#22806;&#25253;&#21578;&#65306;<\/p><pre><code>protected $dontReport = [\n    InvalidArgumentException::class,\n    IlluminateAuthAuthenticationException::class\n];<\/code><\/pre><h2>&#33258;&#23450;&#20041;HTTP&#38169;&#35823;&#39029;&#38754;<\/h2><p>Laravel &#20801;&#35768;&#24744;&#36731;&#26494;&#21019;&#24314;&#38024;&#23545;&#19981;&#21516; HTTP &#29366;&#24577;&#30721;&#30340;&#33258;&#23450;&#20041;&#38169;&#35823;&#39029;&#38754;&#12290;&#20363;&#22914;&#65292;&#23545;&#20110; 404 &#38169;&#35823;&#39029;&#38754;&#65292;&#21487;&#20197;&#21019;&#24314;&#19968;&#20010;&#12290;<code>404.blade.php<\/code>&#35831;&#25552;&#20379;&#26356;&#22810;&#20449;&#24687;&#25110;&#20855;&#20307;&#38382;&#39064;&#65292;&#20197;&#20415;&#25105;&#20026;&#24744;&#25552;&#20379;&#20934;&#30830;&#30340;&#32763;&#35793;&#21644;&#24110;&#21161;&#12290;<code>resources\/views\/errors\/<\/code>&#30446;&#24405;&#12290;&#24403;&#21457;&#29983;404&#24322;&#24120;&#26102;&#65292;Laravel&#20250;&#33258;&#21160;&#20351;&#29992;&#27492;&#35270;&#22270;&#12290;<\/p><h2>&#32467;&#35770;&#12290;<\/h2><p>&#36825;&#20010;&#25945;&#31243;&#21521;&#24744;&#23637;&#31034;&#20102;Laravel&#20869;&#32622;&#30340;&#24322;&#24120;&#22788;&#29702;&#33021;&#21147;&#20043;&#26053;&#12290;&#24744;&#23398;&#20064;&#20102;&#22914;&#20309;&#26377;&#25928;&#22320;&#24037;&#20316;&#65292;&#20197;&#20415;&#22312;&#21019;&#24314;&#33021;&#22815;&#20248;&#38597;&#22320;&#22788;&#29702;&#24847;&#22806;&#24773;&#20917;&#30340;&#24212;&#29992;&#31243;&#24207;&#26102;&#36827;&#34892;&#24322;&#24120;&#22788;&#29702;&#12290;&#35760;&#20303;&#65292;&#33391;&#22909;&#30340;&#24322;&#24120;&#22788;&#29702;&#23545;&#20110;&#32500;&#25252;&#39640;&#36136;&#37327;&#30340;&#29992;&#25143;&#20307;&#39564;&#20197;&#21450;&#30830;&#20445;&#24212;&#29992;&#31243;&#24207;&#21487;&#38752;&#24615;&#21644;&#31283;&#23450;&#24615;&#33267;&#20851;&#37325;&#35201;&#12290;<\/p><\/body>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div><div class=\"wp-block-columns p-0 border is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-columns px-4 py-3 border-bottom has-background is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\" style=\"background:linear-gradient(243deg,rgb(238,238,238) 0%,rgba(58,166,242,0.15) 100%)\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<div class=\"wp-block-group is-nowrap is-layout-flex wp-container-core-group-is-layout-6c531013 wp-block-group-is-layout-flex\">\r\n<figure class=\"wp-block-image size-thumbnail is-resized is-style-rounded is-style-rounded--3\"><img decoding=\"async\" src=\"https:\/\/www.zhaozhao123.cn\/myitems\/images\/sites16\/2025\/06\/xygcfznnzczhsdmwydzhsmzzzmddnq-400x300.jpg\" alt=\"&#38271;&#24037;&#30721;&#22900;1523\" class=\"wp-image-2906\" style=\"object-fit:cover;width:30px;height:30px\"><\/figure>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading my-0\" style=\"font-size:clamp(0.875rem, 0.875rem + ((1vw - 0.2rem) * 0.175), 1rem);\">&#38271;&#24037;&#30721;&#22900;1523<\/h2>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n\r\n\r\n\r\n<div class=\"wp-block-columns px-xl-5 px-4 py-xl-4 py-3 is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\r\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\r\n<p>&#22312; Laravel &#20013;&#22788;&#29702;&#24322;&#24120;&#26159;&#26500;&#24314;&#20581;&#22766;&#30340; API &#24212;&#29992;&#31243;&#24207;&#30340;&#20851;&#38190;&#37096;&#20998;&#12290;&#36890;&#36807;&#27491;&#30830;&#22320;&#20351;&#29992; <code>try-catch<\/code> &#32467;&#26500;&#21644; <code>catch-all<\/code> &#36335;&#24452;&#65292;&#20320;&#21487;&#20197;&#26377;&#25928;&#22320;&#31649;&#29702;&#21508;&#31181;&#24322;&#24120;&#24773;&#20917;&#12290;<\/p>\n<h3>1. &#24322;&#24120;&#22788;&#29702;&#30340;&#22522;&#26412;&#27010;&#24565;<\/h3>\n<p>&#22312; Laravel &#20013;&#65292;&#24322;&#24120;&#22788;&#29702;&#20027;&#35201;&#20998;&#20026;&#20004;&#31181;&#31867;&#22411;&#65306;<\/p>\n<ul>\n<li><strong>&#33258;&#23450;&#20041;&#38169;&#35823;<\/strong>&#65288;&#36890;&#24120;&#29992;&#20110;&#29305;&#23450;&#19994;&#21153;&#36923;&#36753;&#38169;&#35823;&#65289;<\/li>\n<li><strong>&#36890;&#29992;&#24322;&#24120;&#22788;&#29702;&#22120;<\/strong>&#65288;&#36866;&#29992;&#20110;&#22823;&#37096;&#20998;&#24322;&#24120;&#24773;&#20917;&#65289;<\/li>\n<\/ul>\n<h3>2. &#33258;&#23450;&#20041;&#38169;&#35823;&#22788;&#29702;<\/h3>\n<p>&#21019;&#24314;&#19968;&#20010;&#26032;&#30340;&#25511;&#21046;&#22120;&#26041;&#27861;&#26469;&#22788;&#29702;&#33258;&#23450;&#20041;&#38169;&#35823;&#12290;&#20363;&#22914;&#65306;<\/p>\n<pre><code class=\"language-php\">&lt;?php\n\nnamespace AppHttpControllers;\n\nuse IlluminateFoundationBusDispatchesJobs;\nuse IlluminateRoutingController as BaseController;\nuse IlluminateFoundationValidationValidatesRequests;\nuse IlluminateFoundationAuthAccessAuthorizesRequests;\n\nclass CustomErrorController extends Controller\n{\n    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;\n\n    public function handleCustomError($error)\n    {\n        return response()-&gt;json(['message' =&gt; $error], 500);\n    }\n}<\/code><\/pre>\n<p>&#36825;&#20010;&#25511;&#21046;&#22120;&#23558;&#25509;&#21463;&#19968;&#20010;&#38169;&#35823;&#23545;&#35937;&#20316;&#20026;&#21442;&#25968;&#65292;&#24182;&#23558;&#20854;&#36716;&#25442;&#20026; JSON &#26684;&#24335;&#24182;&#36820;&#22238; 500 &#29366;&#24577;&#30721;&#12290;<\/p>\n<h3>3. &#20351;&#29992; catch-all &#22788;&#29702;&#22120;<\/h3>\n<p>&#23545;&#20110;&#26356;&#19968;&#33324;&#30340;&#24322;&#24120;&#24773;&#20917;&#65292;&#21487;&#20197;&#20351;&#29992; <code>catch-all<\/code> &#36335;&#24452;&#26469;&#25429;&#33719;&#25152;&#26377;&#30340;&#38750;&#33258;&#23450;&#20041;&#38169;&#35823;&#12290;&#36825;&#36890;&#24120;&#19982; Laravel &#30340; <code>IlluminateContractsHttpResponse<\/code> &#26694;&#26550;&#19968;&#36215;&#20351;&#29992;&#12290;<\/p>\n<p>&#22312; <code>config\/app.php<\/code> &#25991;&#20214;&#20013;&#28155;&#21152;&#20197;&#19979;&#37197;&#32622;&#65306;<\/p>\n<pre><code class=\"language-ini\">'exception-handling' =&gt; [\n    'handler' =&gt; 'IlluminateContractsDebugExceptionHandler',\n],<\/code><\/pre>\n<p>&#28982;&#21518;&#22312;&#38656;&#35201;&#30340;&#22320;&#26041;&#24341;&#20837;&#30456;&#24212;&#30340; <code>ExceptionHandler<\/code> &#31867;&#65306;<\/p>\n<pre><code class=\"language-php\">&lt;?php namespace AppExceptions;\n\nuse Exception;\n\nclass GeneralException extends Exception\n{\n    protected $handler = null;\n\n    public function __construct(Exception $e)\n    {\n        parent::__construct($e-&gt;getMessage(), $e-&gt;getCode(), $e-&gt;getTrace());\n        $this-&gt;handler = new AppExceptionsGeneralExceptionHandler();\n    }\n\n    public function getHandler()\n    {\n        return $this-&gt;handler;\n    }\n}\n\nclass GeneralExceptionHandler implements IlluminateContractsDebugExceptionHandler\n{\n    public function report(Exception $exception)\n    {\n        \/\/ Handle the exception here.\n    }\n\n    public function render($request, Exception $exception)\n    {\n        return response()-&gt;view('errors.404', ['title' =&gt; 'Not Found']);\n    }\n}<\/code><\/pre>\n<h3>&#31034;&#20363;&#20195;&#30721;<\/h3>\n<p>&#19979;&#38754;&#26159;&#19968;&#20010;&#31616;&#21333;&#30340;&#20363;&#23376;&#65292;&#23637;&#31034;&#20102;&#22914;&#20309;&#22312; Laravel &#20013;&#21019;&#24314;&#19968;&#20010;&#33258;&#23450;&#20041;&#24322;&#24120;&#22788;&#29702;&#22120;&#65306;<\/p>\n<pre><code class=\"language-php\">&lt;?php namespace AppExceptions;\n\nuse Exception;\n\nclass CustomException extends Exception\n{\n    public function render($request, Exception $exception)\n    {\n        return response()-&gt;view('errors.500', ['title' =&gt; 'Internal Server Error'])-&gt;header('Content-Type', 'application\/json');\n    }\n}<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;&#25105;&#20204;&#21019;&#24314;&#20102;&#19968;&#20010;&#21517;&#20026; <code>CustomException<\/code> &#30340;&#33258;&#23450;&#20041;&#24322;&#24120;&#22788;&#29702;&#22120;&#65292;&#23427;&#20250;&#22312;&#21709;&#24212;&#20013;&#21253;&#21547;&#19968;&#20010; 500 &#38169;&#35823;&#29366;&#24577;&#30721;&#65292;&#24182;&#35774;&#32622; Content-Type &#20026; <code>application\/json<\/code>&#12290;<\/p>\n<h3>&#24635;&#32467;<\/h3>\n<p>&#22312; Laravel &#20013;&#22788;&#29702;&#24322;&#24120;&#26159;&#19968;&#31181;&#24378;&#22823;&#30340;&#24037;&#20855;&#65292;&#21487;&#20197;&#24110;&#21161;&#20320;&#22312;&#24212;&#29992;&#31243;&#24207;&#20013;&#25429;&#25417;&#21040;&#21508;&#31181;&#38169;&#35823;&#65292;&#24182;&#25552;&#20379;&#21451;&#22909;&#30340;&#29992;&#25143;&#30028;&#38754;&#12290;&#36890;&#36807;&#20351;&#29992;&#36866;&#24403;&#30340;&#24322;&#24120;&#22788;&#29702;&#22120;&#65292;&#20320;&#21487;&#20197;&#30830;&#20445;&#24212;&#29992;&#31243;&#24207;&#21363;&#20351;&#22312;&#36935;&#21040;&#24847;&#22806;&#26102;&#20063;&#33021;&#20445;&#25345;&#31283;&#23450;&#36816;&#34892;&#12290;<\/p>\r\n<\/div>\r\n<\/div>\r\n<\/div>\r\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bug&#32534;&#35793;&#29422; &#22312; Laravel &#20013;&#22788;&#29702;&#24322;&#24120;&#26159;&#30830;&#20445;&#24212;&#29992;&#31243;&#24207;&#31283;&#23450;&#038;#2..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[44],"tuisongtax":[],"class_list":["post-807","my1js","type-my1js","status-publish","hentry","my1js2nav-laravel"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/807","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=807"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=807"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=807"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=807"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}