Yah*_*din 7 laravel laravel-5 laravel-5.1
我想要一个自定义500错误页面.这可以通过创建视图来完成errors/500.blade.php
.
这对于生产模式来说很好,但是在调试模式下我看不到默认的异常/调试页面(看起来是灰色的并且说"哎呀出错了").
因此,我的问题是:如何生成自定义500错误页面,但是当调试模式为真时,原始500错误页面?
只需在 \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);
}
}
Run Code Online (Sandbox Code Playgroud)
或者
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);
}
}
Run Code Online (Sandbox Code Playgroud)
APP_DEBUG=false
从 Laravel 5.5+ 开始,如果您有views/errors/500.blade.php 文件,这将自动发生。
https://github.com/laravel/framework/pull/18481
我发现解决我的问题的最佳方法是将以下函数添加到 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
}
Run Code Online (Sandbox Code Playgroud)
欢迎更好的解决方案!