laravel ÇëÇóÒì³£´¦Öóͷ£
laravel ÊÇÒ»ÖÖÊ¢ÐÐµÄ php ¿ò¼Ü£¬ËüÌṩÁËÒ»¸öÇ¿Ê¢¶øÎÞаµÄϵͳÀ´¹¹½¨ web Ó¦ÓóÌÐò¡£¿ÉÊÇ£¬ÔÚ¿ª·¢Àú³ÌÖУ¬ÄÑÃâ»áÓöµ½ÇëÇóÒì³£µÄÇéÐΡ£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÌÖÂÛ laravel ÇëÇóÒì³£µÄ´¦Öóͷ£ÒªÁì¡£
Òì³£µÄ·ÖÀà
Laravel ÖÐÇëÇóÒì³£¿ÉÒÔ·ÖΪÁ½ÖÖÀàÐÍ£º³ÌÐòÒì³£ºÍ HTTP Òì³£¡£
³ÌÐòÒì³£ÊÇÔÚ´úÂëÔËÐÐÀú³ÌÖзºÆðµÄÒì³££¬ÀýÈç PHP Å׳öµÄÖÂÃü¹ýʧ£¬Î´²¶»ñµÄÒì³£µÈµÈ¡£
HTTP Òì³£ÊÇÖ¸ÔÚ HTTP ÇëÇóÖзºÆðµÄÒì³££¬ÀýÈç 404 Not Found£¬500 Internal Server Error µÈµÈ¡£
²î±ðÀàÐ͵ÄÒì³£ÐèÒª½ÓÄɲî±ðµÄ´¦Öóͷ£·½·¨¡£
³ÌÐòÒì³£µÄ´¦Öóͷ£
³ÌÐòÒì³£¿ÉÄ᷺ܻÆðÔÚ Laravel ¿ØÖÆÆ÷ÖУ¬ÈôÊDz»¼Ó´¦Öóͷ££¬½«»áµ¯³öÒ»¸öÒ³ÃæÏÔʾ¹ýʧÐÂÎÅ¡£Õâ²¢²»ÊÇÓû§ÆÚÍû¿´µ½µÄ£¬Òò´ËÐèÒª¶Ô³ÌÐòÒì³£¾ÙÐд¦Öóͷ£¡£
Laravel ¸øÎÒÃÇÌṩÁËÁ½ÖÖÒªÁì´¦Öóͷ£³ÌÐòÒì³£¡£µÚÒ»ÖÖÊÇʹÓÃÒì³£´¦Öóͷ£Æ÷£¬µÚ¶þÖÖÊÇʹÓÃÈ«¾ÖÒì³£´¦Öóͷ£¡£
2.1 Òì³£´¦Öóͷ£Æ÷
Laravel Òì³£´¦Öóͷ£Æ÷ÊÇÒ»¸öÀ࣬´¦Öóͷ£Ó¦ÓóÌÐòÅ׳öµÄÒì³£¡£ÈôÊÇÎÒÃÇÏëÒªÅ׳öÒ쳣ʱ¿ØÖÆÆ÷·µ»ØÒ»¸ö JSON ÃûÌõÄÏìÓ¦£¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸ö×Ô½ç˵Òì³£´¦Öóͷ£Æ÷¡£ÏÂÃæÊÇÒ»¸öÀý×Ó£º
<?php namespace AppExceptions; use Exception; use IlluminateFoundationExceptionsHandler as ExceptionHandler; class Handler extends ExceptionHandler { /** * A list of the exception types that are not reported. * * @var array */ protected $dontReport = [ // ]; /** * Report or log an exception. * * @param Exception $exception * @return void */ public function report(Exception $exception) { parent::report($exception); } /** * Render an exception into an HTTP response. * * @param IlluminateHttpRequest $request * @param Exception $exception * @return IlluminateHttpResponse */ public function render($request, Exception $exception) { if ($exception instanceof IlluminateDatabaseEloquentModelNotFoundException) { return response()->json([ 'error' => 'Resource not found' ], 404); } return parent::render($request, $exception); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃǼÌÐøÁË Laravel µÄÒì³£´¦Öóͷ£Æ÷À࣬²¢ÖØдÁË render ÒªÁì¡£ÔÚ render ÒªÁìÖУ¬ÎÒÃǼì²éÁËÒì³£ÀàÐÍÊÇ·ñÊÇ IlluminateDatabaseEloquentModelNotFoundException¡£ÈôÊÇÊÇ£¬ÎÒÃÇ·µ»ØÒ»¸ö JSON ÃûÌõÄÏìÓ¦¡£
ÎÒÃÇ»¹¿ÉÒÔÔÚÕâ¸öÒªÁìÖд¦Öóͷ£ÆäËûµÄ³ÌÐòÒì³£¡£ÕâÖÖ´¦Öóͷ£·½·¨µÄÀûÒæÊÇ£¬ÎÒÃÇ¿ÉÒÔΪÿÖÖÀàÐ͵ÄÒì³£±àд×Ô½ç˵µÄ´¦Öóͷ£Æ÷¡£ÕâÑùÎÒÃǾÍÄܹ»Õ¹Íûµ½ÎÒÃÇ»á»ñµÃʲôÑùµÄÏìÓ¦¡£
2.2 È«¾ÖÒì³£´¦Öóͷ£
ʹÓÃÈ«¾ÖÒì³£´¦Öóͷ££¬ÎÒÃÇ¿ÉÒÔ²¶»ñÓ¦ÓóÌÐòÖеÄËùÓÐÒì³££¬¶ø²»ÊÇΪÿÖÖÒì³£±àдµ¥¶ÀµÄ´¦Öóͷ£Æ÷¡£ÏÂÃæÊÇÒ»¸öÀý×Ó£º
<?php namespace AppExceptions; use Exception; use IlluminateFoundationExceptionsHandler as ExceptionHandler; class Handler extends ExceptionHandler { /** * A list of the exception types that are not reported. * * @var array */ protected $dontReport = [ // ]; /** * Report or log an exception. * * @param Exception $exception * @return void */ public function report(Exception $exception) { parent::report($exception); } /** * Render an exception into an HTTP response. * * @param IlluminateHttpRequest $request * @param Exception $exception * @return IlluminateHttpResponse */ public function render($request, Exception $exception) { if ($exception instanceof SymfonyComponentHttpKernelExceptionHttpException) { $code = $exception->getStatusCode(); return response()->json([ 'error' => 'HTTP Exception', 'status' => $code ], $code); } return parent::render($request, $exception); } /** * Render the given HttpException. * * @param SymfonyComponentHttpKernelExceptionHttpException $e * @return IlluminateHttpResponse */ protected function renderHttpException(HttpException $e) { $status = $e->getStatusCode(); if (view()->exists("errors.{$status}")) { return response()->view("errors.{$status}", ['exception' => $e], $status, $e->getHeaders()); } else { return $this->convertExceptionToResponse($e); } } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇÖØдÁË render ÒªÁ죬¼ì²éÒì³£ÀàÐÍÊÇ·ñÊÇ SymfonyComponentHttpKernelExceptionHttpException¡£ÈôÊÇÊÇ£¬ÎÒÃǽ¨ÉèÁËÒ»¸ö JSON ÃûÌõÄÏìÓ¦£¬°üÀ¨¹ýʧÐÂÎÅºÍ HTTP ״̬Âë¡£
ÈôÊÇÎÒÃÇÐèÒª·ºÆð HTML Ò³Ã棬ÎÒÃÇ»¹¿ÉÒÔÖØд renderHttpException ÒªÁ죬ÒÔäÖȾ×Ô½ç˵µÄÒì³£Ò³Ãæ¡£
HTTP Òì³£µÄ´¦Öóͷ£
Laravel ÌṩÁËÒ»ÖÖ¼òÆÓµÄÒªÁì´¦Öóͷ£ HTTP Òì³£¡£Í¨¹ý×Ô½ç˵ app/Exceptions/Handler.php ÖÐµÄ render ÒªÁ죬ÎÒÃÇ¿ÉÒÔ·µ»ØÖ¸¶¨µÄ HTTP ״̬Âë¡£ÒÔÏÂÊÇÒ»¸öÀý×Ó£º
public function render($request, Exception $exception) { if ($this->isHttpException($exception)) { return $this->renderHttpException($exception); } else { return parent::render($request, $exception); } } protected function renderHttpException(HttpException $exception) { return response()->view('errors.' . $exception->getStatusCode(), [], $exception->getStatusCode()); }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄÀý×ÓÖУ¬ÎÒÃǼì²éÒì³£ÊÇ·ñÊÇ HTTP Òì³£¡£ÈôÊÇÊÇ£¬ÎÒÃÇʹÓà getStatusCode ÒªÁì»ñÈ¡ HTTP ״̬Â룬²¢½«ÆäÓÃ×÷ÊÓͼÃû³Æ¡£ÔÚÕâ¸öÀý×ÓÖУ¬ÎÒÃÇÖ»ÊÇ·µ»ØÁËÒ»¸ö¶ÔӦ״̬ÂëµÄÊÓͼ¡£
½áÂÛ
ÔÚ±¾ÎÄÖУ¬ÎÒÃÇÏÈÈÝÁË Laravel ÖгÌÐòºÍ HTTP Òì³£µÄ´¦Öóͷ£ÒªÁì¡£ÎÒÃÇѧϰÁËÔõÑùʹÓÃÒì³£´¦Öóͷ£Æ÷ºÍÈ«¾ÖÒì³£´¦Öóͷ£À´´¦Öóͷ£³ÌÐòÒì³££¬ÒÔ¼°ÔõÑù×Ô½ç˵ render ÒªÁìÀ´´¦Öóͷ£ HTTP Òì³£¡£¹ØÓÚ Laravel ¿ª·¢Ö°Ô±À´Ëµ£¬×¼È·´¦Öóͷ£Òì³£ÊǺÜÊÇÖ÷ÒªµÄ¡£Í¨¹ýʹÓÃÕâЩÊÖÒÕ£¬ÎÒÃÇÄܹ»Ô½·¢×¼È·µØ¿ØÖÆÓ¦ÓóÌÐòµÄÐÐΪ£¬Ìá¸ßÆä¿É¿¿ÐÔºÍÎȹÌÐÔ¡£
ÒÔÉϾÍÊÇlaravel ÇëÇóÒì³£´¦Öóͷ£µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡