尊龙凯时人生就是搏

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¡¢ÍøÕ¾µØͼ