laravel ¹ýʧ´¦Àí
laravelÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼Ü£¬ËüÌṩÁËÐí¶à¹ýʧ´¦Àí»úÖÆ£¬×ÊÖú¿ª·¢Õ߸üÇáËɵص÷ÊԺʹ¦Àí³ÌÐòÖеĹýʧ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«Ì½ÌÖlaravelµÄ¹ýʧ´¦Àí»úÖÆ¡£
ÉèÖÃÎļþ
LaravelµÄÉèÖÃÎļþconfig/app.phpÖÐÓÐÒ»¸ökeyֵΪdebugµÄÑ¡Ïî¡£µ±debugΪtrueʱ£¬Laravel»áÏÔʾÏêϸµÄ¹ýʧÐÅÏ¢£¬±ãÓÚ¿ª·¢Ö°Ô±µ÷ÊÔ³ÌÐò¡£µ±ÉèÖÃΪfalseʱ£¬ÔòÖ»ÏÔʾһÑùƽ³£µÄ¹ýʧÐÅÏ¢¡£
Òì³£´¦Àí
LaravelÖÐʹÓÃÒì³£´¦ÀíÀ´²¶»ñ´úÂëÖеĹýʧ¡£µ±³ÌÐòÖб¬·¢¹ýʧʱ£¬Laravel»áÅ׳öÒ»¸öÒì³££¬Õâ¸öÒì³£½«±»ÏµÍ³´¦Àí¡£
ÎÒÃÇ¿ÉÒÔÔÚapp/Exceptions/Handler.phpÎļþÖбàд´úÂëÀ´´¦ÀíÒì³£¡£ÔÚÕâ¸öÎļþÖУ¬ÓÐÒ»¸öÒªÁì½Ð×örender£¬Õâ¸öÒªÌå»á±»×Ô¶¯Å²ÓÃÒÔäÖȾÒì³£¡£ÎÒÃÇ¿ÉÒÔÔÚÕâ¸öÒªÁìÖнç˵ÈçÄÇÀïÖÃÒì³£¡£ÏÂÃæÊÇÒ»¸öÀý×Ó£º
public function render($request, Exception $exception) { if ($exception instanceof IlluminateDatabaseEloquentModelNotFoundException) { return response()->json([ 'error' => 'Resource not found' ], 404); } return parent::render($request, $exception); }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇÅжÏÁËÒì³£ÀàÐÍ£¬ÈôÊÇÊÇModelNotFoundException£¬¾Í·µ»ØÒ»¸ö404¹ýʧºÍÒ»¸öjsonÃûÌõÄÏìÓ¦£¬¸æËß¿Í»§¶Ë×ÊԴδÕÒµ½¡£ÈôÊDz»ÊÇÕâ¸öÒì³£ÀàÐÍ£¬¾ÍŲÓø¸ÀàµÄrenderÒªÁì¾ÙÐд¦Àí¡£
ÈÕÖ¾¼Í¼
³ýÁËÔÚ¿ØÖÆ̨»òÕßä¯ÀÀÆ÷ÉÏÏÔʾ¹ýʧÐÅÏ¢£¬Laravel»¹Ö§³Ö½«¹ýʧÐÅÏ¢¼Í¼µ½ÈÕÖ¾ÎļþÖС£ÕâÑù¿ÉÒÔ¸üÀû±ãµØ×·×ٺ͵÷ÊÔ³ÌÐòÖеĹýʧ¡£
LaravelĬÈÏʹÓÃMonolog¿âÀ´¼Í¼ÈÕÖ¾¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÐÞ¸ÄÉèÖÃÎļþÖеÄÈÕ־ѡÏîÀ´ÉèÖÃÈÕÖ¾¼Í¼¼¶±ðºÍÈÕÖ¾ÎļþλÖá£
'log' => env('APP_LOG', 'single'), 'log_level' => env('APP_LOG_LEVEL', 'debug'), 'channels' => [ 'stack' => [ 'driver' => 'stack', 'channels' => ['single'], ], 'single' => [ 'driver' => 'single', 'path' => storage_path('logs/laravel.log'), 'level' => 'debug', ], ]
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÉèÖÃÎļþÖУ¬ÎÒÃÇ¿ÉÒÔ¿´µ½Laravel»á½«ÈÕÖ¾¼Í¼µ½/storage/logs/laravel.logÎļþÖУ¬²¢ÇҼͼËùÓÐdebug¼¶±ð¼°ÒÔÉϵÄÈÕÖ¾¡£
×Ô½ç˵¹ýʧҳÃæ
µ±³ÌÐòÖб¬·¢¹ýʧʱ£¬Ä¬ÈÏÇéÐÎÏÂLaravel»áÏÔʾһ¸ö¼òÆӵĹýʧ½çÃæ¡£¿ÉÊÇÎÒÃÇÒ²¿ÉÒÔ×Ô½ç˵¹ýʧҳÃ棬ÒÔµÖ´ï¸üÓÅÃÀºÍ×Ô¼ºÐèÒªµÄЧ¹û¡£
ÎÒÃÇ¿ÉÒÔ½¨Éè×Ô¼ºµÄÊÓͼÎļþ£¬²¢½«ËüÃÇ·ÅÔÚresources/views/errorsĿ¼Ï¡£È»ºóÔÚapp/Exceptions/Handler.phpÎļþÖÐÖØдrenderÒªÁ죬µ±±¬·¢Ò쳣ʱÏÔʾÎÒÃÇ×Ô½ç˵µÄÊÓͼҳÃ档ʾÀý´úÂëÈçÏ£º
public function render($request, Exception $exception) { if ($this->isHttpException($exception)) { if ($exception->getStatusCode() == 404) { return response()->view('errors.404', [], 404); } } return parent::render($request, $exception); }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǽç˵ÁËÒ»¸ö404¹ýʧҳÃ棬²¢ÔÚrenderÒªÁìÖÐÅжÏ״̬ÂëÊÇ·ñΪ404¡£ÈôÊÇÊÇ£¬¾Í·µ»ØÎÒÃǸոս¨ÉèµÄÊÓͼҳÃæ¡£
×ܽá
LaravelÌṩÁËÐí¶à¹ýʧ´¦Àí»úÖÆ£¬Ê¹¿ª·¢Ö°Ô±¸üÇáËɵش¦Àí¹ýʧ¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃÎļþ¡¢Òì³£´¦Àí¡¢ÈÕÖ¾¼Í¼ºÍ×Ô½ç˵¹ýʧҳÃæµÈ·½·¨À´ÕƿسÌÐòÖÐËùÓйýʧ¡£
ËäÈ»£¬ÓÅÒìµÄ³ÌÐòÔ±Òª¾¡¿ÉÄÜïÔ̳ÌÐòÖеĹýʧ·ºÆð£¬×èÖ¹ÔÚºóÆÚ²ÅÐÐÖ¹ÖÃÏ¢Õù¾ö¹ýʧ£¬ÓÉÓÚ¾¡¿ÉÄÜïÔ̹ýʧµÄ·ºÆ𣬲»µ«¿ÉÒÔÌá¸ß³ÌÐòÐÔÄÜ£¬»¹¿ÉÒÔ´ó´óïÔ̺óÆÚά»¤µÄ¾«Éñ±¾Ç®¡£
ÒÔÉϾÍÊÇlaravel ¹ýʧ´¦ÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡