Laravel 5 错误处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27889647/
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
Laravel 5 Error Handling
提问by Jordan Dobrev
I am using Laravel 5 and I am trying to make custom 404 page and custom Exception handling, but I can't figure out where to put my code. Some time ago there was an ErrorServiceProvider that no longer exists. Can anyone give me some pointers?
我正在使用 Laravel 5,我正在尝试制作自定义 404 页面和自定义异常处理,但我不知道将代码放在哪里。前段时间有一个不再存在的 ErrorServiceProvider。谁能给我一些指点?
EDIT: I saw they have added a Handler class in the App/Exception folder but that still seems not the right place to put it because it does not follow at all the laravel 4.2 App::error, App::missing and App::fatal methods. Anyone has any ideas?
编辑:我看到他们在 App/Exception 文件夹中添加了一个 Handler 类,但这似乎仍然不是放置它的正确位置,因为它根本不遵循 laravel 4.2 App::error、App::missing 和 App::致命的方法。任何人有任何想法?
采纳答案by Jordan Dobrev
Use app/Exceptions/Handler.php render method to achieve that. L5 documentation http://laravel.com/docs/5.0/errors#handling-errors
使用 app/Exceptions/Handler.php 渲染方法来实现。L5 文档http://laravel.com/docs/5.0/errors#handling-errors
public function render($request, Exception $e)
{
if ($e instanceof Error) {
if ($request->ajax()) {
return response(['error' => $e->getMessage()], 400);
} else {
return $e->getMessage();
}
}
if ($this->isHttpException($e)) {
return $this->renderHttpException($e);
} else {
return parent::render($request, $e);
}
}
回答by unliu
Here is how to customize your error page while complying with the APP_DEBUG
setting in .env
.
下面是如何定制错误页面,同时与遵守APP_DEBUG
的设置.env
。
app/Exceptions/Handler.php
应用程序/异常/Handler.php
public function render($request, Exception $e)
{
if ($this->isHttpException($e))
{
return $this->renderHttpException($e);
}
else
{
if (env('APP_DEBUG'))
{
return parent::render($request, $e);
}
return response()->view('errors.500', [], 500);
}
}