ÔõÑùÐÞ¸ÄLaravelµÄ±¨´íÊä³öÐÎʽ
laravelÊÇÒ»¿îÊ¢ÐеÄphp¿ò¼Ü£¬Ðí¶à¿ª·¢Ö°Ô±¶¼Ñ¡ÔñʹÓÃËüÀ´¿ª·¢webÓ¦ÓóÌÐò¡£ÔÚ¿ª·¢Àú³ÌÖУ¬ÄÑÃâ»áÓöµ½Ò»Ð©¹ýʧºÍÒì³£¡£Ä¬ÈÏÇéÐÎÏ£¬laravel»á½«¹ýʧÐÅÏ¢Êä³öµ½phpµÄ¹ýʧÈÕÖ¾ÖУ¬Õâ¹ØÓÚ¿ª·¢Ö°Ô±À´Ëµ²¢²»ÊǺÜÀû±ã¡£Òò´Ë£¬ÔÚ±¾ÎÄÖн«ÏÈÈÝÔõÑùÐÞ¸ÄlaravelµÄ±¨´íÊä³öÐÎʽ¡£
ÐÞ¸ÄÉèÖÃÎļþ
LaravelµÄ¹ýʧÐÅÏ¢Êä³öÐÎʽ¿ÉÒÔÔÚÉèÖÃÎļþÖоÙÐÐÐ޸ġ£·¿ªÏîÄ¿¸ùĿ¼Ï嵀 .env Îļþ£¬ÔÚ APP_DEBUG ÕâÒ»ÐÐÖн«Öµ¸ÄΪ true ¼´¿É¿ªÆôµ÷ÊÔģʽ£¬Í¬Ê±LaravelÒ²»á½«±¨´íÐÅÏ¢Ö±½ÓÊä³öµ½ä¯ÀÀÆ÷¶Ë¡£
APP_DEBUG=true
µÇ¼ºó¸´ÖÆ
È»¶ø£¬ÕâÖÖÒªÁì±£´æ×ÅÇå¾²Òþ»¼¡£ÓÉÓÚÔÚµ÷ÊÔģʽÏ»ὫӦÓóÌÐòµÄϸ½ÚÐÅÏ¢ËùÓÐ̻¶³öÀ´£¬Õâ¿ÉÄÜ»áÈù¥»÷ÕßÓÐʱ»ú¶ÔÓ¦ÓóÌÐò¾ÙÐй¥»÷¡£Òò´Ë£¬ÔÚÉú²úÇéÐÎÏ£¬Ó¦¸Ã½ûÓõ÷ÊÔģʽ£¬½« APP_DEBUG µÄÖµ¸ÄΪ false¡£
APP_DEBUG=false
µÇ¼ºó¸´ÖÆ
µ«ÕâÑùÐ޸ĺ󣬱¨´íÐÅÏ¢ÈÔÈ»ÎÞ·¨ÔÚä¯ÀÀÆ÷¶ËÏÔʾ£¬ÐèÒª¾ÙÐнøÒ»²½ÉèÖá£
×°ÖÃWhoops°ü
WhoopsÊÇÒ»¸öPHP¹ýʧ´¦Öóͷ£¿â£¬¿ÉÒÔ½«¹ýʧÐÅÏ¢ÒÔ¸üÑŹ۵ķ½·¨Õ¹Ê¾³öÀ´¡£LaravelĬÈϼ¯³ÉÁËWhoops£¬¿ÉÊÇÐèҪװÖÃËüµÄÒÀÀµ°ü¡£
ÔÚÏîÄ¿¸ùĿ¼ÏÂʹÓÃComposer¾ÙÐÐ×°Öãº
composer require filp/whoops
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬ÐèÒªÔÚ App\Exceptions\Handler ÀàµÄ render ÒªÁìÖоÙÐйýʧÐÅÏ¢µÄÃÀ»¯´¦Öóͷ£¡£
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\Response; use Whoops\Exception\Inspector; use Whoops\Handler\PrettyPageHandler; class Handler extends ExceptionHandler { public function render($request, Exception $exception) { $response = new Response(); if (config('app.debug')) { $whoops = new \Whoops\Run; $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler); $response->setContent($whoops->handleException($exception)); } else { $response->setContent($this->renderHttpException($exception)); } $response->setStatusCode($this->isHttpException($exception) ? $exception->getStatusCode() : 500); $response->exception = $exception; return $response; } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄ´úÂëÖУ¬ÈôÊÇÓ¦ÓóÌÐò´¦ÓÚµ÷ÊÔģʽ£¬ÔòʹÓÃWhoops¿âµÄ PrettyPageHandler ¾ÙÐÐÃÀ»¯´¦Öóͷ££¬²»È»Ôò²»×ö´¦Öóͷ£¡£
×Ô½ç˵±¨´íÒ³Ãæ
³ýÁËʹÓÃWhoops¿â£¬Laravel»¹ÌṩÁËÒ»ÖÖÒªÁ죬¿ÉÒÔÉÁ¿ª·¢Ö°Ô±×Ô½ç˵±¨´íÒ³Ãæ¡£Ö»ÐèÔÚ resources/views/errors Ŀ¼Ï½¨ÉèÏà¹ØÄ£°åÎļþ¼´¿É¡£
ÀýÈ磬ÈôÊÇÓ¦ÓóÌÐò±¬·¢404¹ýʧ£¬Laravel»áÔÚ resources/views/errors/404.blade.php ÖвéÕÒÄ£°åÎļþ£¬ÈôÊÇÕÒ²»µ½ÔòʹÓÃĬÈϵÄÄ£°åÎļþ¡£
nbsp;html> <meta> <title>404 Not Found</title> <h1>404</h1> <p>Page not found.</p>
µÇ¼ºó¸´ÖÆ
ͨ¹ýÕâÖÖ·½·¨£¬¿ª·¢Ö°Ô±¿ÉÒÔ×Ô½ç˵±¨´íÒ³Ã棬ÈÃÆäÔ½·¢ÇкÏÓ¦ÓóÌÐòµÄÆø¸Å¡£
×ܽá
ÔÚ¿ª·¢Àú³ÌÖУ¬ÍêÉƵı¨´í»úÖÆ¿ÉÒÔ×ÊÖú¿ª·¢Ö°Ô±¸ü¿ìµØ¶¨Î»²¢ÐÞ¸´¹ýʧ¡£±¾ÎÄÏÈÈÝÁ˼¸ÖÖÐÞ¸ÄLaravel±¨´íÊä³öÐÎʽµÄÒªÁ죺ÐÞ¸ÄÉèÖÃÎļþ¡¢×°ÖÃWhoops°üºÍ×Ô½ç˵±¨´íÒ³Ã档ͨ¹ýÕâЩҪÁ죬¿ª·¢Ö°Ô±¿ÉÒÔÕÒµ½×îÊʺÏ×Ô¼ºµÄ±¨´íÊä³öÐÎʽ£¬Ìá¸ß¿ª·¢Ð§ÂʺʹúÂëÖÊÁ¿¡£
ÒÔÉϾÍÊÇÔõÑùÐÞ¸ÄLaravelµÄ±¨´íÊä³öÐÎʽµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡