PHPackages                             back/api-response - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. back/api-response

ActiveLibrary[API Development](/categories/api)

back/api-response
=================

统一 API 响应包

1.0.2(3y ago)21.1kMITPHPPHP ^7.4|^8.1|^8.0

Since Jun 8Pushed 3y ago1 watchersCompare

[ Source](https://github.com/haoqianlei/api-response)[ Packagist](https://packagist.org/packages/back/api-response)[ Docs](https://github.com/back/api-response)[ RSS](/packages/back-api-response/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (10)Dependencies (4)Versions (15)Used By (0)

Api 统一响应处理
==========

[](#api-统一响应处理)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b66a2873db8c170e0cc791430b1f4e00ce0301e1d0216a9fa37602729e25e8b5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6261636b2f6170692d726573706f6e73652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/back/api-response)[![Total Downloads](https://camo.githubusercontent.com/ad0eafcb2a1de80eab304e672fd10099e0783e6ccca269f4bf325cf0b350d4f2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6261636b2f6170692d726573706f6e73652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/back/api-response)

团队内部使用的`API`响应处理包

安装
--

[](#安装)

你可以通过`composer`进行安装

```
composer require back/api-response
```

使用
--

[](#使用)

1. 生成配置文件

```
 php artisan vendor:publish --provider="Back\ApiResponse\ApiResponseServiceProvider"
```

2. 修改`app/Http/Controllers/Controller.php`

```
namespace App\Http\Controllers;

use Back\ApiResponse\ResponseHandler; // 引入
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    // 引入 ResponseHandler Trait
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests, ResponseHandler;
}
```

3. 在控制器中使用得方法，前提是必须拿`app/Http/Controllers/Controller.php`作为父类，代码编写方式请自行查看`你的项目/vendor/back/api-response/ResponseHandler.php`

```
// 成功通知
// 这个方法， data，msg 可以自己进行控制可选
$this->respond($data, $msg);

// 自定义成功消息通知
// 这个方法，默认返回成功。data 数据默认为 null，msg 信息自行控制
$this->respondWithMessage($msg);

// 错误方法
// 错误方法，可以传递 api_code，系统根据 api_code 自行查找对应的文字说明，具体看你是否配置了文字对应关系
$this->respondBadRequest($api_code);

// 自定义错误消息通知
// 这个方法，默认返回成功。$api_code 数据必传，msg 信息自行控制，如果不传默认去找 $api_code 对应得消息通知
$this->respondWithErrorMessage($api_code, $msg);

// 错误提示
// 错误方法，可以传递 api_code，系统根据 api_code 必传自行查找对应的文字说明，可以控制 http_code 必传。比如 人脸识别失败，我要返回 200 状态码。
$this->respondWithError($api_code, $http_code);

// 身份验证失败
// 如果账号密码错误，或者登录失效都可以是用此方法，参数必传
$this->respondUnAuthorizedRequest($api_code);

// 数据不存在
// 数据不存在时可以使用此方法，参数必传
$this->respondNotFound($api_code);
```

4.接管异常处理，修改`app/Exceptions/Handler.php`

1. 引入

```
use Back\ApiResponse\Exceptions\BackExceptionHandler;
```

2. 如果没有`render`方法，那么就增加这个方法。如果有那么修改此方法为下面内容

```
public function render($request, Throwable $e)
{
    return BackExceptionHandler::render($request, $e);
}
```

### 修改日志

[](#修改日志)

请查看[CHANGELOG](CHANGELOG.md)来观看最近发生的变化

贡献
--

[](#贡献)

有关详细信息请观看[CONTRIBUTING](CONTRIBUTING.md)

### 安全

[](#安全)

如果您发现任何与安全相关的问题，请发送电子邮件`1300657068@qq.com`而不是使用`issues`。

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~47 days

Recently: every ~147 days

Total

14

Last Release

1230d ago

Major Versions

0.10 → 1.0.02021-06-30

PHP version history (3 changes)0.0.1PHP ^7.4|^8.0

1.0.1PHP ^7.4|^8.1

1.0.2PHP ^7.4|^8.1|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/1f714c96f4871c245b679c6c7312a8034f0c4c1cc51e940c91c31c43569e14a4?d=identicon)[a1300657068](/maintainers/a1300657068)

---

Top Contributors

[![haoqianlei](https://avatars.githubusercontent.com/u/22309178?v=4)](https://github.com/haoqianlei "haoqianlei (22 commits)")

---

Tags

Backapi-response

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/back-api-response/health.svg)

```
[![Health](https://phpackages.com/badges/back-api-response/health.svg)](https://phpackages.com/packages/back-api-response)
```

###  Alternatives

[defstudio/telegraph

A laravel facade to interact with Telegram Bots

815320.5k3](/packages/defstudio-telegraph)[riclep/laravel-storyblok

A Laravel wrapper around the Storyblok API to provide a familiar experience for Laravel devs

6277.0k5](/packages/riclep-laravel-storyblok)[jasara/php-amzn-selling-partner-api

A fluent interface for Amazon's Selling Partner API in PHP

1348.1k1](/packages/jasara-php-amzn-selling-partner-api)[rapidez/core

Rapidez Core

1822.4k65](/packages/rapidez-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
