添加仅适用于Laravel生产的自定义500错误页面 [英] Add custom 500 error page only for production in Laravel

查看:411
本文介绍了添加仅适用于Laravel生产的自定义500错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个自定义500错误页面.只需在errors/500.blade.php中创建视图即可完成此操作.

I want to have a custom 500 error page. This can be done simply by creating a view in 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").

因此,我的问题是:如何在生产中有一个自定义的500错误页面,而在调试模式为true时如何显示原始的500错误页面?

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?

推荐答案

我发现解决问题的最好方法是在App\Exceptions\Handler.php

I found the best way to solve my problem is to add the following function to 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!

这篇关于添加仅适用于Laravel生产的自定义500错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆