Php 在维护视图页面下显示laravel应用程序中所有错误/异常的站点

Php 在维护视图页面下显示laravel应用程序中所有错误/异常的站点,php,laravel,exception,error-handling,Php,Laravel,Exception,Error Handling,我是拉威尔的初学者。我遇到了一个laravel的申请。在这方面,我需要处理所有类型的异常/错误。异常,如ViewExceptions、ErrorExceptions等。我需要为所有这些系统异常、错误以及所有数据库或编码异常和错误显示一个查看页面(维护中的站点) 我已经查过了,还在谷歌上搜索解决方案。但我搜索的越多,我就对解决方案感到困惑。由于应用程序已经投入生产,我无法对每个控制器进行更改以处理异常。我猜,我只需要在App/Exception/Handler类中进行更改,但不确定如何工作 我得到

我是拉威尔的初学者。我遇到了一个laravel的申请。在这方面,我需要处理所有类型的异常/错误。异常,如ViewExceptions、ErrorExceptions等。我需要为所有这些系统异常、错误以及所有数据库或编码异常和错误显示一个查看页面(维护中的站点)

我已经查过了,还在谷歌上搜索解决方案。但我搜索的越多,我就对解决方案感到困惑。由于应用程序已经投入生产,我无法对每个控制器进行更改以处理异常。我猜,我只需要在App/Exception/Handler类中进行更改,但不确定如何工作

我得到的表单搜索结果表明,我必须在处理程序类中进行更改:

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Throwable  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Throwable $exception)
{
    if ($exception instanceof CustomException) {
        return response()->view('errors.site_down', [], 500);
    }

    return parent::render($request, $exception);
}
如果存在ViewException,则上述代码不显示

我注意到在.env APP_DEBUG中是true,而在config/APP中是false。这会影响你吗

如何将所有异常或错误重定向到站点\下一页?另外,请指导我在laravel中处理异常和错误。我越来越糊涂了


提前感谢。

请不要使用if语句:

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Throwable  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Throwable $exception)
{
    return response()->view('errors.site_down', [], 503);
}
如果您试图声明站点已停止维护,则可能还需要返回503


在对这种方法的批评中,我认为声称网站因您的错误而处于维护状态对您的用户来说是不诚实和透明的,从长远来看,这不会带来回报。

只要去掉if声明:

/**
 * Render an exception into an HTTP response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Throwable  $exception
 * @return \Illuminate\Http\Response
 */
public function render($request, Throwable $exception)
{
    return response()->view('errors.site_down', [], 503);
}
如果您试图声明站点已停止维护,则可能还需要返回503


在对这种方法的批评中,我认为对用户来说,声称网站正在维护您的错误是不诚实和透明的,而且从长远来看,这不会带来回报。

资源/views/errors/503.blade.php

<?php

namespace App\Exceptions;

use Exception;

class CustomException extends Exception
{
    //
}
<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use App\Exceptions\CustomException as CustomException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Throwable  $exception
     * @return void
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Throwable $exception)
    {
        // Thrown when a custom exception occurs.
        if ($exception instanceof CustomException) {
            return response()->view('error.page.path', [], 500);
        }

        // Thrown when an exception occurs.
        if ($exception instanceof Exception) {
            response()->view('errors.page.path', [], 500);
        }

        return parent::render($request, $exception);
    }
}
您可以使用
vendor:publish
Artisan命令发布Laravel的错误页面模板。发布模板后,您可以根据自己的喜好对其进行自定义:

php artisan vendor:publish --tag=laravel-errors
此命令将在
resources/views/errors/
目录中创建所有自定义错误页。您可以根据需要进行自定义


请参阅官方文档

resources/views/errors/503.blade.php上添加刀片页面

<?php

namespace App\Exceptions;

use Exception;

class CustomException extends Exception
{
    //
}
<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use App\Exceptions\CustomException as CustomException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Throwable  $exception
     * @return void
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Throwable $exception)
    {
        // Thrown when a custom exception occurs.
        if ($exception instanceof CustomException) {
            return response()->view('error.page.path', [], 500);
        }

        // Thrown when an exception occurs.
        if ($exception instanceof Exception) {
            response()->view('errors.page.path', [], 500);
        }

        return parent::render($request, $exception);
    }
}
您可以使用
vendor:publish
Artisan命令发布Laravel的错误页面模板。发布模板后,您可以根据自己的喜好对其进行自定义:

php artisan vendor:publish --tag=laravel-errors
此命令将在
resources/views/errors/
目录中创建所有自定义错误页。您可以根据需要进行自定义


请参阅官方文档

了解自定义异常首先,您必须在exceptions文件夹
App\exceptions\CustomException.php

<?php

namespace App\Exceptions;

use Exception;

class CustomException extends Exception
{
    //
}
<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use App\Exceptions\CustomException as CustomException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Throwable  $exception
     * @return void
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Throwable $exception)
    {
        // Thrown when a custom exception occurs.
        if ($exception instanceof CustomException) {
            return response()->view('error.page.path', [], 500);
        }

        // Thrown when an exception occurs.
        if ($exception instanceof Exception) {
            response()->view('errors.page.path', [], 500);
        }

        return parent::render($request, $exception);
    }
}
记住
使用App\Exceptions\CustomException自定义异常文件,您需要在其中抛出自定义异常,如下所示:

use App\Exceptions\CustomException;

function test(){
    throw new CustomException('This is an error');
}

对于自定义异常,首先您必须在异常文件夹
App\exceptions\CustomException.php

<?php

namespace App\Exceptions;

use Exception;

class CustomException extends Exception
{
    //
}
<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use App\Exceptions\CustomException as CustomException;
use Throwable;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that are not reported.
     *
     * @var array
     */
    protected $dontReport = [
        //
    ];

    /**
     * A list of the inputs that are never flashed for validation exceptions.
     *
     * @var array
     */
    protected $dontFlash = [
        'password',
        'password_confirmation',
    ];

    /**
     * Report or log an exception.
     *
     * @param  \Throwable  $exception
     * @return void
     */
    public function report(Throwable $exception)
    {
        parent::report($exception);
    }

    /**
     * Render an exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Throwable  $exception
     * @return \Illuminate\Http\Response
     */
    public function render($request, Throwable $exception)
    {
        // Thrown when a custom exception occurs.
        if ($exception instanceof CustomException) {
            return response()->view('error.page.path', [], 500);
        }

        // Thrown when an exception occurs.
        if ($exception instanceof Exception) {
            response()->view('errors.page.path', [], 500);
        }

        return parent::render($request, $exception);
    }
}
记住
使用App\Exceptions\CustomException自定义异常文件,您需要在其中抛出自定义异常,如下所示:

use App\Exceptions\CustomException;

function test(){
    throw new CustomException('This is an error');
}

您也可以对
401
403,
404
419
429
500
503
是的,您的解决方案要好得多,以相同的方式处理错误,但只呈现“相同”视图。您也可以对
401
403,
404
419
,执行此操作,
429
500
503
是的,您的解决方案更好,以相同的方式处理错误,但只呈现“相同”的视图。如果我使用
return response()->view('errors.site_down')而不是
返回父::render($request,$exception)
在我的
App\Exceptions\Handler.php
中,这会影响应用程序功能吗?我不需要在站点的下一页显示特定的错误代码,如500503等。我需要在所有出现的异常情况下使用它,这样访问者将无法看到开发人员的代码。是的,但您仍将返回响应代码。如果您试图声称它因维护而停机,那么503就是代码。我不是足够的专家,无法告诉您哪些功能可能会被阻止,我最大的担心是错误日志记录。如果
php artisan停机
,那么它就会消失503@Kurt修士对不起,我没有听到你的第一句话。我可以更改Handler.php的
return
?事实上,我遇到了“如果你的应用程序遇到错误,你可能会开始得到空白页,如果你弄乱了handler.php”这句话,所以我有点害怕这样做。如果我使用
return response()->view('errors.site_down')而不是
返回父::render($request,$exception)
在我的
App\Exceptions\Handler.php
中,这会影响应用程序功能吗?我不需要在站点的下一页显示特定的错误代码,如500503等。我需要在所有出现的异常情况下使用它,这样访问者将无法看到开发人员的代码。是的,但您仍将返回响应代码。如果您试图声称它因维护而停机,那么503就是代码。我不是足够的专家,无法告诉您哪些功能可能会被阻止,我最大的担心是错误日志记录。如果
php artisan停机
,那么它就会消失503@Kurt修士对不起,我没有听到你的第一句话。我可以更改Handler.php的
return
?事实上,我遇到了这样一句话:“当你的应用程序遇到错误时,如果你弄乱了handler.php,你可能会得到一个空白页面”,所以我有点害怕这样做。