定制 radweb/json-exception-formatter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

radweb/json-exception-formatter

Composer 安装命令:

composer require radweb/json-exception-formatter

包简介

JSON for your Exceptions

README 文档

README

Build Status Latest Stable Version License

Laravel JSON Exception Formatter

A small Laravel package to format & output exceptions in JSON format when required.

By default in Laravel, throwing an Exception in debug mode will display a nice JSON response when required (eg. an AJAX response, or an Accept: application/javascript header).

However once you're not in debug mode (ie. a production environment), a whole HTML response is displayed instead.

With this package, when you're not in debug mode, exceptions will be output as JSON (only without debug information like the file name & line number).

NOTE This does NOT affect HTML requests. Only AJAX/JSON requests are altered.

Installation

Add radweb/json-exception-formatter to your composer.json file.

{
    "require": {
        "radweb/json-exception-formatter": "dev-master"
    }
}

In app/config/app.php, add the Service Provider to the providers array:

array(
    'providers' => array(
        // ...
        'Radweb\JsonExceptionFormatter\JsonExceptionFormatterServiceProvider',
    )
)

Custom Formatters

You can override the default JSON exception formatter to use a different format, or provide more detail in the output.

To override, implement the Radweb\JsonExceptionFormatter\FormatterInterface interface, and bind with the IoC container. This requires you to implement two methods: formatDebug() and formatPlain().

Example implementation:

<?php

use Radweb\JsonExceptionFormatter\FormatterInterface;

class CustomDebugFormatter implements FormatterInterface {

    public function formatDebug(Exception $e)
    {
        return array(
            'theError' => array(
                'message' => $e->getMessage(),
                'detail' => 'In file '.$e->getFile().' on line '.$e->getLine(),
            ),
        );
    }

    public function formatPlain(Exception $e)
    {
        return array(
            'theError' => array(
                'message' => $e->getMessage(),
                // we don't want to display debug details in production
            ),
        );
    }

}

Now we just have to bind it in the IoC container. Add this anywhere in your app's bootstrap code (if you have nowhere, routes.php will do):

App::bind('Radweb\JsonExceptionFormatter\FormatterInterface', 'CustomDebugFormatter');

Preview

Normal Request, Debug Mode ENABLED

Normal Request, Debug Mode DISABLED

JSON Request, Debug Mode ENABLED

JSON Request, Debug Mode DISABLED

统计信息

  • 总下载量: 2.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 6
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-02-13

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固