尊龙凯时人生就是博

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 ÇëÇóÒì³£´¦ÀíµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼ ¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ