承接 mohamed-ahmed/api-response 相关项目开发

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

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

mohamed-ahmed/api-response

最新稳定版本:2.0.0

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
  }
}

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固