Laravel 5 上未显示自定义错误页面
Custom error page not showing on Laravel 5
我正在尝试显示自定义错误页面而不是默认的 Laravel 5 消息:
"Whoops...looks like something went wrong"
我在发帖之前进行了大量搜索,我尝试了这个解决方案,它应该适用于 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";
但我得到相同的结果:空白页
在您的 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');
}
执行此操作的典型方法是 create individual views for each error type。
我想要一个动态的自定义错误页面(这样所有错误都会出现在同一个 blade 模板中)。
在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 个错误页面。
我有两个错误页面 - 404.blade.php & generic.blade.php
我想要:
- 显示所有缺失页面的 404 页
- 开发中异常的异常页面
- 生产中异常的一般错误页面
我正在使用 .env - APP_DEBUG 来决定这个。
我更新了异常处理程序中的渲染方法:
app/Exceptions/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);
}
在 Larvel 5.2 上 app/exceptions/handler.php
只需扩展此方法 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);
}
}
我强烈同意每个想要在 Laravel 中自定义错误体验的人,这样他们的用户就不会看到令人尴尬的消息,例如 'Whoops, looks like something went wrong.'
我花了 永远 才弄明白这个问题。
如何在 Laravel 5.3
中自定义 "Whoops" 消息
在 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 页面,假设您有一个。
在 laravel 5.4 中,您可以将此代码块放在 Handler.php 中的 render 函数中 - 在 app/exceptions/Handler.[=18= 中找到]
//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);
我正在尝试显示自定义错误页面而不是默认的 Laravel 5 消息:
"Whoops...looks like something went wrong"
我在发帖之前进行了大量搜索,我尝试了这个解决方案,它应该适用于 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";
但我得到相同的结果:空白页
在您的 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');
}
执行此操作的典型方法是 create individual views for each error type。
我想要一个动态的自定义错误页面(这样所有错误都会出现在同一个 blade 模板中)。
在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 个错误页面。
我有两个错误页面 - 404.blade.php & generic.blade.php
我想要:
- 显示所有缺失页面的 404 页
- 开发中异常的异常页面
- 生产中异常的一般错误页面
我正在使用 .env - APP_DEBUG 来决定这个。
我更新了异常处理程序中的渲染方法:
app/Exceptions/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);
}
在 Larvel 5.2 上 app/exceptions/handler.php
只需扩展此方法 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);
}
}
我强烈同意每个想要在 Laravel 中自定义错误体验的人,这样他们的用户就不会看到令人尴尬的消息,例如 'Whoops, looks like something went wrong.'
我花了 永远 才弄明白这个问题。
如何在 Laravel 5.3
中自定义 "Whoops" 消息在 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 页面,假设您有一个。
在 laravel 5.4 中,您可以将此代码块放在 Handler.php 中的 render 函数中 - 在 app/exceptions/Handler.[=18= 中找到]
//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);