mohamed-ahmed/api-response 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

mohamed-ahmed/api-response

Composer 安装命令:

composer require mohamed-ahmed/api-response

包简介

Reusable trait for standardized API responses

README 文档

README

A reusable Laravel package for building consistent JSON API responses.
It helps you standardize your API outputs with data, pagination, exceptions, and error structures — cleanly and efficiently.

Package info

URL: https://packagist.org/packages/mohamedahmed/api-response

📦 Installation

composer require mohamedahmed/api-response

🚀 Usage

Changelog

v2.0.0

  • Added DataTrait for handling response data.
  • Added PaginationTrait with support for Laravel pagination.
  • Enabled method chaining: ->paginate($paginator)
  • Enabled method chaining: ->messages([...])->add($message)->merge([messages])
  • Enabled method chaining: ->errors([...])->add($serror)->merge([errors])
  • Enabled method chaining: ->alerts([...])->add($alert)->merge([alerts])

In your Laravel controller:

use MohamedAhmed\ApiResponse\Traits\ResponseApi;

class SomeController extends Controller
{
    use ResponseApi;

    public function index()
    {
        $this->setData(SomeModel::all());
        return $this->apiResponse();
    }
}

### v2.0.0

$res = Response::make();

🧠 Available Methods

setData($data)

Sets the main response data.

If data is paginated, pagination details are auto-included.

$this->setData(User::all());
$this->setData(User::paginate(10));

### v2.0.0

$res->data($items->items());
$res->paginate($items);

setException(Throwable $exception)

Stores an exception and adds error info in the response.

try {
    // logic
} catch (\Exception $e) {
    $this->setException($e);
    $this->setCode(500);
    return $this->apiResponse();
}

### v2.0.0

$res->exception($e);
return $this->tojson();

setEc(int $code)

Sets an internal error code, useful for debugging and frontend error mapping.

$this->setEc(1001); // Internal reference code

setMessages(array|string $messages)

Custom messages for the user.

$this->setMessages(['Created successfully']);

### v2.0.0

$res->nessages(['Created successfully']); // for alerts and errors also
$res->add('additional message);
$res->merge('[more of messages]);

setCode(int $httpCode)

Sets HTTP status code (defaults to 200).

$this->setCode(201); // HTTP Created

### v2.0.0
$res->code(201);

apiResponse()

Builds and returns the full JSON response.

return $this->apiResponse();

### v2.0.0
return $res->tojson();

✅ Example Response

{
  "code": 200,
  "responseStatus": true,
  "messages": [],
  "response": {
    "dataLength": 3,
    "pagination": {
      "path": "/api/products?page=1",
      "total": 50,
      "perPage": 10,
      "currentPage": 1,
      "lastPage": 5
    },
    "data": [{}, {}, {}]
  },
  "error": {
    "errorCode": null,
    "line": null,
    "errorMessage": null,
    "file": null
  }
}

mohamed-ahmed/api-response 适用场景与选型建议

mohamed-ahmed/api-response 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 mohamed-ahmed/api-response 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 30
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-24