承接 lanin/laravel-api-exceptions 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

lanin/laravel-api-exceptions

Composer 安装命令:

composer require lanin/laravel-api-exceptions

包简介

All in one solution for exception for JSON REST APIs on Laravel and Lumen.

README 文档

README

Travis

All in one solution for exception for JSON REST APIs on Laravel and Lumen.

About

The goal of this package is to provide you with a set of most common exceptions that may be needed while developing JSON REST API. It also:

  • Handles exceptions output.
  • Handles exceptions report to logs.
  • Overwrites default Validator to make validation errors more verbose.
  • Has a FormRequest that to handle validation errors and pass them to ApiExceptions layer.

Installation

PHP 5.4+ or HHVM 3.3+, Composer and Laravel 5.1+ are required.

To get the latest version of Laravel Laravel-API-Debugger, simply add the following line to the require block of your composer.json file.

For Laravel 5.1

"lanin/laravel-api-exceptions": "^0.1.0"

For Laravel 5.3

"lanin/laravel-api-exceptions": "^1.0.0"

You'll then need to run composer install or composer update to download it and have the autoloader updated.

Once Laravel-API-Exceptions is installed, if you're using Laravel 5.1, 5.2, 5.3, and 5.4, you need to register the service provider. Open up config/app.php and add the following to the providers key.

Lanin\Laravel\ApiExceptions\ApiExceptionsServiceProvider::class,

If you're using Laravel 5.5, you don't need to add anything in config/app.php. It will use package auto discovery feature in Laravel 5.5.

Exceptions

Every ApiException can be thrown as a normal exception and they will be automatically serialized to JSON with corresponding HTTP status, if user wants json:

{
    "id": "not_found",
    "message": "Requested object not found."
}

This object will be also populated with trace info, when APP_DEBUG is true.

Also it can have meta attribute when there is additional info. For example for validation errors:

{
	"id": "validation_failed",
	"message": "Validation failed.",
	"meta": {
		"errors": {
			"tags": [{
				"rule": "max.array",
				"message": "The tags may not have more than 10 items.",
				"parameters": ["10"]
			}]
		}
	}
}

For ValidationApiException, meta attribute has errors object that contains validations errors. Every attribute of this object is a name of a request parameter to validate to and value is an array of errors with description.

Customization

You can customize errors APU response by overriding formatApiResponse method in your ExceptionsHandler.

For example if you want to put everything under error attribute, you can do it like this:

/**
 * Format error message for API response.
 *
 * @param  ApiException  $exception
 * @return mixed
 */
protected function formatApiResponse(ApiException $exception)
{
    return [
        'error' => $exception->toArray(),
    ];
}

Handler

Extend your default exceptions handler with:

  • \Lanin\Laravel\ApiExceptions\LaravelExceptionHandler for Laravel
  • \Lanin\Laravel\ApiExceptions\LumenExceptionHandler for Lumen

And remove everything else. Example:

<?php

namespace App\Exceptions;

use Lanin\Laravel\ApiExceptions\LaravelExceptionHandler;

class Handler extends LaravelExceptionHandler
{

}

FormRequest

To use FormRequest extend all your Request classes with \Lanin\Laravel\ApiExceptions\Support\Request. It will automatically support validation errors and pass them to the output.

It also has a very handy helper method validatedOnly() that returns from request only those items that are registered in rules method.

Contributing

Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.

lanin/laravel-api-exceptions 适用场景与选型建议

lanin/laravel-api-exceptions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 104.37k 次下载、GitHub Stars 达 40, 最近一次更新时间为 2016 年 06 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「framework」 「json」 「exceptions」 「api」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lanin/laravel-api-exceptions 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lanin/laravel-api-exceptions 我们能提供哪些服务?
定制开发 / 二次开发

基于 lanin/laravel-api-exceptions 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 104.37k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 40
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 40
  • Watchers: 2
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-06-30