hiteshpadhara/laravel-response-macro
Composer 安装命令:
composer require hiteshpadhara/laravel-response-macro
包简介
Adds a Response::api() macro for uniform JSON APIs
README 文档
README
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
- Fork the repository.
- Create your feature branch:
git checkout -b feature/YourFeature. - Commit your changes:
git commit -m 'Add some feature'. - Push to the branch:
git push origin feature/YourFeature. - Submit a pull request.
Licensing
This package is open-sourced software licensed under the MIT license.
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-21