Laravel 5上未显示自定义错误页面

问题描述 投票:7回答:6

我试图显示自定义错误页面而不是默认的Laravel 5消息:

“哎呀,看起来像出事了”

我在发布之前做了很多搜索,我尝试了这个解决方案,它应该适用于Laravel 5,但没有运气:https://laracasts.com/discuss/channels/laravel/change-whoops-looks-like-something-went-wrong-page

这是我在app/Exceptions/Handler.php文件中的确切代码:

<?php namespace App\Exceptions;

use Exception;
use View;
use Bugsnag\BugsnagLaravel\BugsnagExceptionHandler as ExceptionHandler;

class Handler extends ExceptionHandler {

    protected $dontReport = [
        'Symfony\Component\HttpKernel\Exception\HttpException'
    ];

    public function report(Exception $e)
    {
        return parent::report($e);
    }

    public function render($request, Exception $e)
    {
        return response()->view('errors.defaultError');
    }

}

但是,不显示我的自定义视图,而是显示空白页面。我也尝试在render()函数中使用此代码

return "Hello, I am an error message";

但我得到了相同的结果:空白页面

php laravel error-handling
6个回答
4
投票

而不是响应在Routes.php中为您的错误页面创建路径,名称为“errors.defaultError”。例如

route::get('error', [
    'as' => 'errors.defaultError',
    'uses' => 'ErrorController@defaultError' ]);

制作控制器或在路线中包含该功能

return view('errors.defaultError');

并使用重定向。例如

public function render($request, Exception $e)
{
    return redirect()->route('errors.defaultError');
}

2
投票

我非常同意每个想要在Laravel中自定义错误体验的人,这样他们的用户就不会看到诸如“哎呀,看起来出错了”等令人尴尬的消息。

我花了很长时间才弄明白这一点。

如何在Laravel 5.3中自定义“哎呀”消息

app/Exceptions/Handler.php中,用这个替换整个prepareResponse函数:

protected function prepareResponse($request, Exception $e)
{        
    if ($this->isHttpException($e)) {            
        return $this->toIlluminateResponse($this->renderHttpException($e), $e);
    } else {
        return response()->view("errors.500", ['exception' => $e]); //By overriding this function, I make Laravel display my custom 500 error page instead of the 'Whoops, looks like something went wrong.' message in Symfony\Component\Debug\ExceptionHandler
    }
}

基本上,它几乎与原始功能相同,但您只是更改else块以呈现视图。

/resources/views/errors,创造500.blade.php

你可以在那里写下你想要的任何文本,但我总是建议保持错误页面非常基本(纯HTML和CSS并没有什么花哨的),这样他们自己几乎没有机会导致进一步的错误。

测试它的工作原理

routes/web.php,您可以添加:

Route::get('error500', function () {
    throw new \Exception('TEST PAGE. This simulated error exception allows testing of the 500 error page.');
});

然后我会浏览到mysite.com/error500,看看你是否看到了自定义的错误页面。

然后还浏览到mysite.com/some-nonexistent-route,看看你是否仍然得到你设置的404页面,假设你有一个。


1
投票

在你的app/exceptions/handler.php上的Larvel 5.2只是扩展这个方法renderHttpException即将这个方法添加到handler.php自定义你想要的

/**
 * Render the given HttpException.
 *
 * @param  \Symfony\Component\HttpKernel\Exception\HttpException  $e
 * @return \Symfony\Component\HttpFoundation\Response
 */
protected function renderHttpException(HttpException $e)
{

   // to get status code ie 404,503
    $status = $e->getStatusCode();

    if (view()->exists("errors.{$status}")) {
        return response()->view("errors.{$status}", ['exception' => $e], $status, $e->getHeaders());
    } else {
        return $this->convertExceptionToResponse($e);
    }
}

1
投票

在laravel 5.4中,您可以将此代码块放在Handler.php中的render函数中 - 在app / exceptions / Handler.php中找到

  //Handle TokenMismatch Error/session('csrf_error')
    if ($exception instanceof TokenMismatchException) {
        return response()->view('auth.login', ['message' => 'any custom message'] );
    }

    if ($this->isHttpException($exception)){       
        if($exception instanceof NotFoundHttpException){
            return response()->view("errors.404");
        }
        return $this->renderHttpException($exception);
    }

    return response()->view("errors.500");
    //return parent::render($request, $exception);

0
投票

我有两个错误页面 - 404.blade.php和generic.blade.php

我想了:

  • 显示所有缺失页面的404页面
  • 开发中的异常的异常页面
  • 生产中例外的通用错误页面

我正在使用.env - APP_DEBUG来决定这一点。

我更新了异常处理程序中的render方法:

应用程序/异常/ Handler.php

public function render($request, Exception $e)
{
    if ($e instanceof ModelNotFoundException) {
        $e = new NotFoundHttpException($e->getMessage(), $e);
    }

    if ($this->isUnauthorizedException($e)) {
        $e = new HttpException(403, $e->getMessage());
    }

    if ($this->isHttpException($e)) {
        // Show error for status code, if it exists
        $status = $e->getStatusCode();
        if (view()->exists("errors.{$status}")) {
            return response()->view("errors.{$status}", ['exception' => $e], $status);
        }
    }

    if (env('APP_DEBUG')) {
        // In development show exception
        return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e);
    }
    // Otherwise show generic error page
    return $this->toIlluminateResponse(response()->view("errors.generic"), $e);

}

-1
投票

这样做的典型方法是create individual views for each error type

我想要一个动态的自定义错误页面(所以所有错误都在同一个刀片模板上)。

在Handler.php我用过:

public function render($request, Exception $e)
{
    // Get error status code.
    $statusCode = method_exists($e, 'getStatusCode') ? $e->getStatusCode() : 400;
    $data = ['customvar'=>'myval'];
    return response()->view('errors.index', $data, $statusCode);
}

然后,我不必为每个可能的http错误状态代码创建20个错误页面。

© www.soinside.com 2019 - 2024. All rights reserved.