Laravel 5 上未显示自定义错误页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30605506/
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
Custom error page not showing on Laravel 5
提问by Emmanuel Scarabin
I am trying to display a custom error page instead of the default Laravel 5 message :
我正在尝试显示自定义错误页面而不是默认的 Laravel 5 消息:
"Whoops...looks like something went wrong"
“哎呀,看起来出事了”
I made a lot of search before posting here, I tried this solution, which should work on Laravel 5 but had no luck with it : https://laracasts.com/discuss/channels/laravel/change-whoops-looks-like-something-went-wrong-page
我在这里发帖之前进行了大量搜索,我尝试了这个解决方案,它应该适用于 Laravel 5,但没有成功:https://laracasts.com/discuss/channels/laravel/change-whoops-looks-like-出错页面
Here is the exact code I have in my app/Exceptions/Handler.php
file :
这是我的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');
}
}
But, instead of displaying my custom view, a blank page is showing. I also tried with this code inside render()
function
但是,不是显示我的自定义视图,而是显示一个空白页面。我也尝试在render()
函数内部使用此代码
return "Hello, I am an error message";
But I get same result : blank page
但我得到相同的结果:空白页
采纳答案by Chris Townsend
Instead of the response create a route for your error page in your Routes.php, with the name 'errors.defaultError'. for example
在 Routes.php 中为您的错误页面创建一个路由,而不是响应,名称为“errors.defaultError”。例如
route::get('error', [
'as' => 'errors.defaultError',
'uses' => 'ErrorController@defaultError' ]);
Either make a controller or include the function in the route with
要么制作控制器,要么在路线中包含该功能
return view('errors.defaultError');
and use a redirect instead. For example
并改用重定向。例如
public function render($request, Exception $e)
{
return redirect()->route('errors.defaultError');
}
回答by Ryan
I strongly agree with everyone who wants to customize the error experience in Laravel such that their users never see an embarrassing message such as 'Whoops, looks like something went wrong.'
我非常同意每个想要在 Laravel 中自定义错误体验的人,这样他们的用户永远不会看到令人尴尬的消息,例如“哎呀,看起来出了问题”。
It took me foreverto figure this out.
我花了很长时间才弄清楚这一点。
How To Customize the "Whoops" Message In Laravel 5.3
如何在 Laravel 5.3 中自定义“Whoops”消息
In app/Exceptions/Handler.php
, replace the entire prepareResponse
function with this one:
在 中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
}
}
Basically, it's almost identical to the original functionality, but you're just changing the else
block to render a view.
基本上,它与原始功能几乎相同,但您只是更改else
块以呈现视图。
In /resources/views/errors
, create 500.blade.php
.
中/resources/views/errors
,创建500.blade.php
。
You can write whatever text you want in there, but I always recommend keeping error pages very basic (pure HTML and CSS and nothing fancy) so that there is almost zero chance that they themselves would cause further errors.
你可以在那里写任何你想要的文本,但我总是建议保持错误页面非常基本(纯 HTML 和 CSS,没有任何花哨的东西),这样它们本身导致进一步错误的可能性几乎为零。
To Test That It Worked
测试它是否有效
In routes/web.php
, you could add:
在 中routes/web.php
,您可以添加:
Route::get('error500', function () {
throw new \Exception('TEST PAGE. This simulated error exception allows testing of the 500 error page.');
});
Then I would browse to mysite.com/error500
and see whether you see your customized error page.
然后我会浏览mysite.com/error500
并查看您是否看到您的自定义错误页面。
Then also browse to mysite.com/some-nonexistent-route
and see whether you still get the 404 page that you've set up, assuming you have one.
然后还浏览mysite.com/some-nonexistent-route
并查看是否仍然获得您设置的 404 页面(假设您有一个)。
回答by Dunsin Olubobokun
In laravel 5.4, you can place this code block inside renderfunction in Handler.php - found in app/exceptions/Handler.php
在 Laravel 5.4 中,您可以将此代码块放置在 Handler.php 的渲染函数中 - 在 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);
回答by Aine
I have two error pages - 404.blade.php & generic.blade.php
我有两个错误页面 - 404.blade.php & generic.blade.php
I wanted:
我想了:
- 404 page to show for all missing pages
- Exception page for exceptions in development
- Generic error page for exceptions in production
- 404 页面显示所有缺失的页面
- 开发中异常的异常页面
- 生产中异常的通用错误页面
I'm using .env - APP_DEBUG to decide this.
我正在使用 .env - APP_DEBUG 来决定这一点。
I updated the render method in the exception handler:
我更新了异常处理程序中的渲染方法:
app/Exceptions/Handler.php
应用程序/异常/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);
}
回答by shakee93
on Larvel 5.2 on your app/exceptions/handler.php
just extend this method renderHttpException
ie add this method to handler.php
customize as you wish
在 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);
}
}
回答by Justin
The typical way to do this is to just create individual views for each error type.
执行此操作的典型方法是仅为每种错误类型创建单独的视图。
I wanted a dynamic custom error page (so all errors hit the same blade template).
我想要一个动态的自定义错误页面(所以所有错误都出现在同一个刀片模板上)。
In Handler.php I used:
在 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);
}
Then I don't have to create 20 error pages for every possible http error status code.
然后我不必为每个可能的 http 错误状态代码创建 20 个错误页面。