hiteshpadhara / laravel-response-macro
Adds a Response::api() macro for uniform JSON APIs
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/hiteshpadhara/laravel-response-macro
Requires
- php: >=8.1
- illuminate/support: ^10.0
Requires (Dev)
- orchestra/testbench: ^8.0
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-12-21 11:48:52 UTC
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.