php 如何在 Laravel 5.2 中显示 500 个内部服务器错误页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41216672/
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-08-26 02:21:11  来源:igfitidea点击:

how to show 500 internal Server error page in laravel 5.2?

phplaravellaravel-5.2

提问by kunal

I want to show the page 500 internal server errorPage. when user had syntax error mistake in project can anyone help me? if i do some mistake in syntax i want to show that particular blade.

我想显示页面500 内部服务器错误页面。当用户在项目中出现语法错误时,有人可以帮助我吗?如果我在语法上犯了一些错误,我想展示那个特定的刀片。

回答by AddWeb Solution Pvt Ltd

You need to create handler to catching FatalErrorExceptionsin your handler like below code:

您需要创建处理程序以捕获FatalErrorExceptions您的处理程序,如下面的代码:

HandlerIn app/Exceptions/Handler.php

处理程序输入app/Exceptions/Handler.php

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

    // 404 page when a model is not found
    if ($e instanceof ModelNotFoundException) {
        return response()->view('errors.404', [], 404);
    }

    // custom error message
    if ($e instanceof \ErrorException) {
        return response()->view('errors.500', [], 500);
    } else {
        return parent::render($request, $e);
    }

    return parent::render($request, $e);
}

ViewSee resources/views/errors/500.blade.php. If not exist then create it.

查看resources/views/errors/500.blade.php。如果不存在则创建它。

You can get more detailed OR other ways from Laravel 5 custom error view for 500

您可以从Laravel 5 自定义错误视图中获取更详细的或其他方式500

回答by Amit Gupta

In your resources/views/errorsfolder create a file named 500.blade.php.

在您的resources/views/errors文件夹中创建一个名为500.blade.php.

Laravel makes it easy to display custom error pages for various HTTP status codes. For example, if you wish to customize the error page for 500 HTTP status codes, create a resources/views/errors/500.blade.php. This file will be served on all 500 errors generated by your application.

Laravel 可以轻松显示各种 HTTP 状态代码的自定义错误页面。例如,如果您希望为 500 个 HTTP 状态代码自定义错误页面,请创建一个 resources/views/errors/500.blade.php. 此文件将用于处理您的应用程序生成的所有 500 个错误。

The problem is that Laravel will only do this automatic rendering of error pages for exceptions that are instances of HttpException. Unfortunately when your server throws an error (method does not exist, variable undefined, etc) it actually throws a FatalErrorException. As such, it is uncaught, and trickles down to the SymfonyDisplayer()which either gives you the trace (debug true) or ugly one-liner 'Whoops, looks like something went wrong' (debug false).

问题是 Laravel 只会为HttpException. 不幸的是,当您的服务器抛出错误(方法不存在、变量未定义等)时,它实际上会抛出一个FatalErrorException. 因此,它没有被捕获,并且会逐渐渗透到SymfonyDisplayer()为您提供跟踪(调试真)或丑陋的单行“哎呀,看起来好像出了点问题”(调试假)。

To solve this you have add this to your rendermethod to app/Exceptions/Handler

为了解决这个问题,您已将其添加到您的render方法中app/Exceptions/Handler

# /app/Exceptions/Handler.php

# use Symfony\Component\Debug\Exception\FlattenException;

# public function render($request, Exception $e)

$exception = FlattenException::create($e);
$statusCode = $exception->getStatusCode($exception);

if ($statusCode === 404 or $statusCode === 500) {
    return response()->view('errors.' . $statusCode, [], $statusCode);
}

Docs

文档

回答by Vedmant

My solution is simple, just replace your render() method in Exceptions\Handler.php file with:

我的解决方案很简单,只需将 Exceptions\Handler.php 文件中的 render() 方法替换为:

/**
 * Render an exception into an HTTP response.
 *
 * @param \Illuminate\Http\Request $request
 * @param \Exception               $exception
 *
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $exception)
{
    if ($request->expectsJson()) {
        return $this->renderJson($request, $exception);
    }

    if ($this->shouldReport($exception) && app()->environment('production')) {
        $exception = new HttpException(500, $exception->getMessage(), $exception);
    }

    return parent::render($request, $exception);
}

It will show 500 page if app in production environment. You will need to have 500.blade.php view in your resources/views/errors folder.

如果应用程序在生产环境中,它将显示 500 页。你需要在你的资源/视图/错误文件夹中有 500.blade.php 视图。

回答by Aurel

    // app/Exceptions/Handler.php

    protected function prepareResponse($request, Exception $e)
    {
        if($this->isHttpException($e) === false && config('app.debug') === false)     {
            $e = new HttpException(500);
        }

        return parent::prepareResponse($request, $e);
    }

Like @Amit said

就像@Amit 说的

The problem is that Laravel will only do this automatic rendering of error pages for exceptions that are instances of HttpException.

问题是 Laravel 只会为属于 HttpException 实例的异常自动渲染错误页面。

So my solution is to replace whatever exception that is not HttpException by a HttpException.

所以我的解决方案是用 HttpException 替换不是 HttpException 的任何异常。

回答by Mody Sharf

in app\Exceptions\Handler create the following method:

在 app\Exceptions\Handler 中创建以下方法:

protected function convertExceptionToResponse(Exception $e)
{
        $e = FlattenException::create($e);
        return response()->view('errors.500', ['exception' => $e], $e->getStatusCode(), $e->getHeaders());
}

it will override the one in the parent class (Illuminate\Foundation\Exceptions\Handler) that displays the whoops page.

它将覆盖显示 whoops 页面的父类 (Illuminate\Foundation\Exceptions\Handler) 中的一个。

回答by Haidang Nguyen

In Laravel 5.4, you could override prepareExceptionfunction in your app\Exception\Handler.php:

在 Laravel 5.4 中,你可以覆盖你的prepareException函数app\Exception\Handler.php

/**
 * @inheridoc
 */
protected function prepareException(Exception $e)
{
    $exception = parent::prepareException($e);

    if(!config('app.debug')) {
        if(!$exception instanceof HttpException && $this->shouldReport($exception)) {
            $exception = new HttpException(500);
        }
    }

    return $exception;
}