添加自定义 500 错误页面仅用于 Laravel 中的生产

问题描述 投票:0回答:8

我想要一个自定义的 500 错误页面。只需在

errors/500.blade.php
中创建视图即可完成此操作。

这对于生产模式来说很好,但在调试模式下我不再获得默认的异常/调试页面(看起来是灰色的,并显示“哎呀出了问题”)。

因此,我的问题是:如何为生产提供自定义的 500 错误页面,但在调试模式为 true 时却是原始的 500 错误页面?

laravel laravel-5 laravel-5.1
8个回答
9
投票

只需在 \App\Exceptions\Handler.php 中添加此代码即可:

public function render($request, Exception $exception)
{
    // Render well-known exceptions here

    // Otherwise display internal error message
    if(!env('APP_DEBUG', false)){
        return view('errors.500');
    } else {
        return parent::render($request, $exception);
    }
}

或者

public function render($request, Exception $exception)
{

    // Render well-known exceptions here

    // Otherwise display internal error message
    if(app()->environment() === 'production') {
        return view('errors.500');
    } else {
        return parent::render($request, $exception);
    }
}

7
投票

从 Laravel 5.5+ 开始,如果您有

APP_DEBUG=false
并有views/errors/500.blade.php 文件,现在会自动发生。

https://github.com/laravel/framework/pull/18481


6
投票

我发现解决我的问题的最好方法是将以下函数添加到

App\Exceptions\Handler.php

protected function renderHttpException(HttpException $e)
{
    if ($e->getStatusCode() === 500 && config('app.debug')) {
        // Display Laravel's default error message with appropriate error information
        return $this->convertExceptionToResponse($e);
    }
    return parent::renderHttpException($e); // Continue as normal 
}

欢迎更好的解决方案!


3
投票

将代码添加到

app/Exceptions/Handler.php
类内的
Handler
文件中:

protected function convertExceptionToResponse(Exception $e)
{
    if (config('app.debug')) {
        return parent::convertExceptionToResponse($e);
    }

    return response()->view('errors.500', [
        'exception' => $e
    ], 500);
}

convertExceptionToResponse
方法可以纠正导致 500 状态的错误。


1
投票

在 Laravel 7+ 中,可以执行以下方法

public function render($request, Throwable $exception)
    {
        if (!env('APP_DEBUG', false)) {
            return response()->view("error500");
        } else {
            return parent::render($request, $exception);
        }
    }

0
投票

将此代码添加到

app/Exceptions/Handler.php
render
方法中。我认为这是干净和简单的。假设您有自定义 500 错误页面。

public function render($request, Exception $e) {
      if ($this->isHttpException($e)) {
        return $this->toIlluminateResponse($this->renderHttpException($e), $e);
    } elseif (!config('app.debug')) {
        return response()->view('errors.500', [], 500);
    } else {
      // return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e);
        return response()->view('errors.500', [], 500);
    }
}
当您需要默认的

whoops 错误页面进行调试时,请使用注释行。使用其他页面来自定义 500 错误页面。


0
投票
在 Laravel

8.x

9.x
 中,您可以使用以下方法:

在项目根文件夹中的“.env”文件中, 将

APP_DEBUG

 设置为 
false

然后在

500.blade.php

 中创建一个名为 
~/resources/views/errors
 的文件
(如果您还没有错误文件夹,请创建它)。

这将在任何 500 服务器错误响应上呈现自定义页面。


0
投票
在 Laravel 9.x 中,最简单的方法是将以下内容添加到

app/Exceptions/Handler.php


protected function getHttpExceptionView(HttpExceptionInterface $e) { // In debug mode we do not want to show our custom error views if (config('app.debug')) { return null; } return parent::getHttpExceptionView($e); }
    
© www.soinside.com 2019 - 2024. All rights reserved.