定制 hiteshpadhara/laravel-response-macro 二次开发

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

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

hiteshpadhara/laravel-response-macro

Composer 安装命令:

composer require hiteshpadhara/laravel-response-macro

包简介

Adds a Response::api() macro for uniform JSON APIs

README 文档

README

Packagist Version Build Status License

Add a Response::api() macro for uniform JSON API responses in Laravel.

Installation

Install the package via Composer:

composer require hiteshpadhara/laravel-response-macro

Laravel ≥10 will auto-discover the service provider. For older versions, register it manually:

// config/app.php
'providers' => [
    // ...
    hiteshpadhara\ResponseMacro\ResponseMacroServiceProvider::class,
];

Usage

Use the macro in your controllers or routes:

use Illuminate\Support\Facades\Response;

return Response::api(200, 'Success', [
    'user' => $user
]);

Generates:

{
  "status": 200,
  "message": "Success",
  "data": {
    "user": { /* ... */ }
  }
}

If the $data parameter is null, the data key is omitted.

Configuration

Publish the config to customize response keys:

php artisan vendor:publish --tag=config

Modify config/response.php:

return [
    'status_key'  => 'status',
    'message_key' => 'message',
    'data_key'    => 'data',
];

Testing

Run the package tests using PHPUnit and Testbench:

composer install
vendor/bin/phpunit

Contributing

  1. Fork the repository.
  2. Create your feature branch: git checkout -b feature/YourFeature.
  3. Commit your changes: git commit -m 'Add some feature'.
  4. Push to the branch: git push origin feature/YourFeature.
  5. Submit a pull request.

Licensing

This package is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-21