alamindev27/laravel-api-responder 问题修复 & 功能扩展

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

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

alamindev27/laravel-api-responder

最新稳定版本:1.0.0

Composer 安装命令:

composer require alamindev27/laravel-api-responder

包简介

A simple and consistent API response helper for Laravel.

README 文档

README

A simple, consistent, and customizable API response helper for Laravel applications.
It provides standard JSON responses for success, error, validation errors, and paginated data.

Features

  • Standardized JSON API responses
  • Success responses with optional custom messages
  • Error responses with HTTP status codes
  • Validation error responses
  • Paginated data responses
  • Configurable via config/api-responder.php
  • Supports Laravel 10, 11, 12
  • Easy to use Facade

Installation

Install via Packagist: https://packagist.org/packages/alamindev27/laravel-api-responder

OR

Require the package using Composer:

composer require alamindev27/laravel-api-responder

Local development: If you are developing locally, use a path repository in your Laravel project composer.json:

"repositories": [
    {
        "type": "path",
        "url": "packages/laravel-api-responder"
    }
]

Then run:

composer update

Publish Config (Optional)

You can publish the config file to customize messages and response structure:

php artisan vendor:publish --provider="Alamindev27\ApiResponder\ApiResponderServiceProvider" --tag=config

This will create config/api-responder.php. You can change default success/error messages, status keys, and pagination keys there.

Usage

Success Response

use Alamindev27\ApiResponder\Facades\ApiResponder;

$datas = [
    'name' => 'MD Al-Amin', 'email' => 'alamindev27@gmail.com', 'age' => 26, 'profession' => 'Web Developer'
];

$meta = [
    "request_id" => "64f8a2c1e4d7a"
];

$message = 'Data fatch Successfully';

return ApiResponder::success($datas, $message, $meta, 200);

Output:

{
    "status": 200,
    "message": "Data fatch Successfully",
    "data": {
        "name": "Al-Amin",
        "email": "alamindev27@gmail.com",
        "age": "27"
    },
    "meta": [
        "request_id":"64f8a2c1e4d7a"
    ]
}

Error Response

    return ApiResponder::error('Something went wrong', [], 400);

Output:

{
    "status": 400,
    "message": "Something went wrong",
    "data": [],
    "meta": []
}

Validation Error Response

return ApiResponder::validationError([
    'email' => ['The email field is required.']
]);

Output:

{
    "status": 422,
    "message": "Validation Error",
    "errors": {
        "email": ["The email field is required."]
    }
}

Paginated Response

$data = User::paginate(10);
$meta = [
    "request_id" => "64f8a2c1e4d7a"
];

$message = 'Pagination data fatch Successfully';

return ApiResponder::paginate($data, $message, $meta, 200);

Output:

{
    "status": 200,
    "message": "Pagination data fatch Successfully",
    "data": [ /* user items */ ],
    "pagination": {
        "total": 100,
        "per_page": 10,
        "current_page": 1,
        "last_page": 10
    }
}

Configuration

The config file config/api-responder.php allows you to customize default response structure:

return [
    'success_status' => true,
    'success_message' => 'Success',
    'error_status' => false,
    'error_message' => 'Something went wrong',
    'pagination_keys' => [
        'data' => 'data',
        'total' => 'total',
        'per_page' => 'per_page',
        'current_page' => 'current_page',
        'last_page' => 'last_page',
    ],
    "meta": [
        "request_id":"64f8a2c1e4d7a"
    ]
];

Testing

This package includes PHPUnit tests using Orchestra Testbench Run tests:

vendor/bin/phpunit

Requirements

PHP 8.1+

Laravel 10, 11, 12

Contributing

Feel free to fork this repository and submit pull requests. Please follow PSR-12 coding standards and write tests for new features.

License

MIT License © MD Al-Amin

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固