Laravel 5错误处理 [英] Laravel 5 Error Handling

查看:107
本文介绍了Laravel 5错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5,并且尝试制作自定义404页面和自定义异常处理,但是我不知道将代码放置在何处。前一段时间,有一个ErrorServiceProvider不再存在。有人可以给我一些指针吗?

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?

编辑:我看到他们在App / Exception文件夹中添加了一个Handler类,但这似乎仍然不合适,因为它根本不遵循laravel 4.2 App :: error,App :: missing和App :: fatal方法。有人有任何想法吗?

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 / Exceptions / Handler.php渲染方法可以实现这一点。 L5文档 http://laravel.com/docs/5.0/errors#handling-errors

Use app/Exceptions/Handler.php render method to achieve that. L5 documentation 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);
    }
}

这篇关于Laravel 5错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆