为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕虎7371278 2023-07-08 17:41:03
我是 Laravel 的初学者。我遇到了一个 Laravel 应用程序。因为我需要处理所有类型的异常/错误。ViewExceptions、ErrorExceptions 等异常。我需要为所有这些系统异常、错误以及所有数据库或编码异常和错误显示一个视图页面(正在维护的站点)。我检查了Laravel 错误处理并在 google 上搜索了解决方案。但我搜索的越多,我对解决方案感到困惑。由于应用程序已经投入生产,我无法更改每个控制器来处理异常。我猜测,我只需要在 App/Exception/Handler 类中进行更改,但不确定它将如何工作。表单搜索我发现我必须像在 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。有影响吗?如何将所有异常或错误重定向到site_down页面?还请指导我 laravel 中的异常和错误处理。我越来越困惑了。提前致谢。
查看完整描述

3 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

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

您可以使用 Artisan 命令发布 Laravel 的错误页面模板vendor:publish。模板发布后,您可以根据自己的喜好对其进行自定义:

php artisan vendor:publish --tag=laravel-errors

此命令将在目录中创建所有自定义错误页面resources/views/errors/。您可以根据需要进行定制。

查看完整回答
反对 回复 2023-07-08
?
ITMISS

TA贡献1871条经验 获得超8个赞

只需去掉 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。


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


查看完整回答
反对 回复 2023-07-08
?
蛊毒传说

TA贡献1895条经验 获得超3个赞

对于自定义异常,首先您必须创建一个自定义异常文件,最好在异常文件夹中App\Exceptions\CustomException.php


<?php


namespace App\Exceptions;


use Exception;


class CustomException extends Exception

{

    //

}

然后在你的异常处理程序文件中App\Exceptions\Handler.php


<?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);

    }

}

请记住use App\Exceptions\CustomException;在需要抛出自定义异常的地方自定义异常文件,如下所示:


use App\Exceptions\CustomException;


function test(){

    throw new CustomException('This is an error');

}


查看完整回答
反对 回复 2023-07-08
  • 3 回答
  • 0 关注
  • 99 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信