PHPackages                             yuanzhihai/think-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. yuanzhihai/think-response

ActiveLibrary[API Development](/categories/api)

yuanzhihai/think-response
=========================

thinkphp api 项目提供一个规范统一的响应数据结构

v1.0.2(2y ago)4105MITPHPPHP ^8.0.2

Since Oct 16Pushed 2y ago1 watchersCompare

[ Source](https://github.com/yuanzhihai/think-response)[ Packagist](https://packagist.org/packages/yuanzhihai/think-response)[ RSS](/packages/yuanzhihai-think-response/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (4)Used By (0)

 think-response
================

[](#-think-response-)

> 为 thinkphp API 项目提供一个规范统一的响应数据格式。

介绍
--

[](#介绍)

`think-response` 主要用来统一 API 开发过程中「成功」、「失败」以及「异常」情况下的响应数据格式。

遵循一定的规范，返回易于理解的 HTTP 状态码，并支持定义 `ResponseCodeEnum` 来满足不同场景下返回描述性的业务操作码。

概览
--

[](#概览)

- 统一的数据响应格式，固定包含：`code`、`status`、`data`、`message`、`error` (响应格式设计源于：[RESTful服务最佳实践](https://www.cnblogs.com/jaxu/p/7908111.html#a_8_2) )
- 你可以继续链式调用 `Response` 类中的所有 public 方法，比如 `Response::success()->header('X-foo','bar');`
- 合理地返回 Http 状态码，默认为 restful 严格模式，可以配置异常时返回 200 http 状态码（多数项目会这样使用）
- 根据 debug 开关，合理返回异常信息、验证异常信息等
- 支持修改 Thinkphp 特地异常的状态码或提示信息，比如将 `No query results for model` 的异常提示修改成 `数据未找到`
- 支持配置返回字段是否显示，以及为她们设置别名，比如，将 `message` 别名设置为 `msg`，或者 分页数据第二层的 `data` 改成 `list`(res.data.data -&gt; res.data.list)
- 内置 Http 标准状态码支持，同时支持扩展 ResponseCodeEnum 来根据不同业务模块定义响应码(可选，需要安装 `yuanzhihai/think-enum`)
- 响应码 code 对应描述信息 message 支持本地化，支持配置多语言(可选，需要安装 `yuanzhihai/think-enum`)

安装
--

[](#安装)

```
composer require yuanzhihai/think-response "^1.0"
composer require yuanzhihai/think-enum "^1.0"  # 可选
```

使用
--

[](#使用)

### 成功响应

[](#成功响应)

#### 示例代码

[](#示例代码)

```
