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

我是 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 中的异常和错误处理。我越来越困惑了。

提前致谢。


慕虎7371278
浏览 206回答 3
3回答

Qyouu

添加刀片页面resources/views/errors/503.blade.php您可以使用 Artisan 命令发布 Laravel 的错误页面模板vendor:publish。模板发布后,您可以根据自己的喜好对其进行自定义:php artisan vendor:publish --tag=laravel-errors此命令将在目录中创建所有自定义错误页面resources/views/errors/。您可以根据需要进行定制。

ITMISS

只需去掉 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。在批评这种方法时,我认为声称网站正在维护您的错误对您的用户来说是不诚实和透明的,从长远来看这不会得到回报。

蛊毒传说

对于自定义异常,首先您必须创建一个自定义异常文件,最好在异常文件夹中App\Exceptions\CustomException.php<?phpnamespace App\Exceptions;use Exception;class CustomException extends Exception{&nbsp; &nbsp; //}然后在你的异常处理程序文件中App\Exceptions\Handler.php<?phpnamespace App\Exceptions;use Exception;use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;use App\Exceptions\CustomException as CustomException;use Throwable;class Handler extends ExceptionHandler{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* A list of the exception types that are not reported.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $dontReport = [&nbsp; &nbsp; &nbsp; &nbsp; //&nbsp; &nbsp; ];&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* A list of the inputs that are never flashed for validation exceptions.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @var array&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $dontFlash = [&nbsp; &nbsp; &nbsp; &nbsp; 'password',&nbsp; &nbsp; &nbsp; &nbsp; 'password_confirmation',&nbsp; &nbsp; ];&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Report or log an exception.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Throwable&nbsp; $exception&nbsp; &nbsp; &nbsp;* @return void&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function report(Throwable $exception)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; parent::report($exception);&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Render an exception into an HTTP response.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param&nbsp; \Illuminate\Http\Request&nbsp; $request&nbsp; &nbsp; &nbsp;* @param&nbsp; \Throwable&nbsp; $exception&nbsp; &nbsp; &nbsp;* @return \Illuminate\Http\Response&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public function render($request, Throwable $exception)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Thrown when a custom exception occurs.&nbsp; &nbsp; &nbsp; &nbsp; if ($exception instanceof CustomException) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return response()->view('error.page.path', [], 500);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Thrown when an exception occurs.&nbsp; &nbsp; &nbsp; &nbsp; if ($exception instanceof Exception) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response()->view('errors.page.path', [], 500);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return parent::render($request, $exception);&nbsp; &nbsp; }}请记住use App\Exceptions\CustomException;在需要抛出自定义异常的地方自定义异常文件,如下所示:use App\Exceptions\CustomException;function test(){&nbsp; &nbsp; throw new CustomException('This is an error');}
打开App,查看更多内容
随时随地看视频慕课网APP