为 Laravel 中的生产添加自定义 500 错误页面

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42625752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:28:50  来源:igfitidea点击:

Add custom 500 error page only for production in Laravel

laravellaravel-5laravel-5.1

提问by Yahya Uddin

I want to have a custom 500 error page. This can be done simply by creating a view in errors/500.blade.php.

我想要一个自定义的 500 错误页面。这可以通过在errors/500.blade.php.

This is fine for production mode, but I no longer get the default exception/ debug pages when in debug mode (the one that looks grey and says "Whoops something went wrong").

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

Therefore, my question is: how can I have a custom 500 error page for production, but the original 500 error page when debug mode is true?

因此,我的问题是:我怎么能有一个自定义的 500 错误页面用于生产,但在调试模式为 true 时原始 500 错误页面?

采纳答案by Yahya Uddin

I found the best way to solve my problem is to add the following function to App\Exceptions\Handler.php

我发现解决我的问题的最佳方法是将以下函数添加到 App\Exceptions\Handler.php

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

Better solutions are welcome!

欢迎更好的解决方案!

回答by Desh901

Simply add this code in \App\Exceptinons\Handler.php:

只需在 \App\Exceptinons\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);
    }
}

Or

或者

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

回答by Chuck Le Butt

From Laravel 5.5+ this will now occur automatically if you have APP_DEBUG=falseand have a views/errors/500.blade.php file.

从 Laravel 5.5+ 开始,如果你APP_DEBUG=false有一个 views/errors/500.blade.php 文件,这会自动发生。

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

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

回答by Finesse

Add the code to the app/Exceptions/Handler.phpfile inside the Handlerclass:

将代码添加到类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);
}

The convertExceptionToResponsemethod gets right such errors that cause the 500 status.

convertExceptionToResponse方法正确处理导致 500 状态的此类错误。

回答by vdeshan

Add this code to the app/Exceptions/Handler.php, rendermethod. I think this is clean and simple. Assuming you have custom 500 error page.

将此代码添加到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);
    }
}

use commented line when you need default whoopserror page for debugging. use other one for custom 500 error page.

当您需要默认的whoops错误页面进行调试时,请使用注释行。使用另一个用于自定义 500 错误页面。