简体   繁体   English

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

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

I want to show the page 500 internal server error Page. 我想显示页面500内部服务器错误页面。 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. 如果我在语法上做了一些错误,我想展示那个特殊的刀片。

You need to create handler to catching FatalErrorExceptions in your handler like below code: 您需要创建处理程序以在处理程序中捕获FatalErrorExceptions ,如下面的代码:

Handler In app/Exceptions/Handler.php 处理 app/Exceptions/Handler.phpapp/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);
}

View See resources/views/errors/500.blade.php . 查看参见resources/views/errors/500.blade.php If not exist then create it. 如果不存在则创建它。

You can get more detailed OR other ways from Laravel 5 custom error view for 500 您可以从Laravel 5自定义错误视图中获取500更详细或其他方式

In your resources/views/errors folder 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. Laravel可以轻松显示各种HTTP状态代码的自定义错误页面。 For example, if you wish to customize the error page for 500 HTTP status codes, create a resources/views/errors/500.blade.php . 例如,如果要为500 HTTP状态代码自定义错误页面,请创建resources/views/errors/500.blade.php This file will be served on all 500 errors generated by your application. 此文件将在您的应用程序生成的所有500个错误中提供。

The problem is that Laravel will only do this automatic rendering of error pages for exceptions that are instances of HttpException . 问题是Laravel只会为HttpException实例的异常自动呈现错误页面。 Unfortunately when your server throws an error (method does not exist, variable undefined, etc) it actually throws a FatalErrorException . 不幸的是,当您的服务器抛出错误(方法不存在,变量未定义等)时,它实际上会抛出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). 因此,它没有被捕获,并且向下移动到SymfonyDisplayer() ,它可以给你跟踪(调试真实)或丑陋的单行'哎呀,看起来像是出错了'(debug false)。

To solve this you have add this to your render method to app/Exceptions/Handler 要解决此问题,您需要将此添加到app/Exceptions/Handler render方法中

# /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 文件

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. 如果应用程序在生产环境中,它将显示500页。 You will need to have 500.blade.php view in your resources/views/errors folder. 您需要在resources / views / errors文件夹中包含500.blade.php视图。

    // 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的异常。

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)中的那个。

In Laravel 5.4, you could override prepareException function in your app\\Exception\\Handler.php : 在Laravel 5.4中,您可以在app\\Exception\\Handler.php覆盖prepareException函数:

/**
 * @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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM