定制 osama-98/laravel-response-api 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

osama-98/laravel-response-api

最新稳定版本:2.0.1

Composer 安装命令:

composer require osama-98/laravel-response-api

包简介

A package for optimizing and unifying the response of laravel API

README 文档

README

Latest Version on Packagist Total Downloads License

A Laravel package that provides a fluent and expressive interface for creating standardized JSON API responses.

Installation

You can install the package via composer:

composer require osama-98/laravel-response-api

Version Compatibility

Package Laravel PHP
2.x 12.x ^8.3
1.x 10.x | 11.x ^8.1

Requirements

  • PHP ^8.3
  • Laravel ^12.0

Response Structure

All responses follow this standardized JSON structure:

{
    "message": "Human-readable message",
    "body": {
        // Response data (optional) 
    },
    "errors": {
        // Validation or error details (optional) 
    }
}

Basic Usage

Success Response

return ApiResponse::success(
    data: ['user' => $user],
    message: 'User retrieved successfully'
);

Response:

{
    "message": "User retrieved successfully",
    "body": {
        "user": {
            "id": 1,
            "name": "John Doe",
            "email": "john@example.com"
        }
    }
}

Error Response

return ApiResponse::error(
    message: 'Validation failed.',
    errors: $validator->errors()->toArray()
);

Response:

{
    "message": "Validation failed.",
    "errors": {
        "email": [
            "The email field must be a valid email address.",
            "The selected email is invalid."
        ]
    }
}

Pagination Support

Using with Resource

return ApiResponse::pagination(
    paginator: User::paginate(10),
    mapper: UserResource::class
);

Using with Custom Mapper

return ApiResponse::pagination(paginator: User::paginate(10), mapper: fn(User $user) => [
    'id' => $user->id,
    'name' => $user->name
]);

Fluent Builder

For more complex responses, use the fluent builder:

return ApiResponse::builder()
    ->data(['key' => 'value'])
    ->message('Custom message')
    ->errors(['field' => 'error message'])
    ->status(Response::HTTP_BAD_REQUEST)
    ->send();

Testing

composer test

This runs:

  • Code style checks (Laravel Pint)
  • Static analysis (PHPStan)
  • Unit tests (Pest PHP)
  • Type coverage tests

Security

If you discover any security-related issues, please email osama.sada98@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固