igorsgm / laravel-api-responses
📮• A Laravel package for building API responses in a normalized and easy to consume way.
v1.0.0
2021-03-08 10:15 UTC
Requires
- php: ^7.1
- illuminate/support: >=6.0
Requires (Dev)
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-12-07 14:32:27 UTC
README
A Laravel package for building API responses in a normalized and easy to consume way.
✨ Features
ApiResponse
Middleware to set theAccept: application/json
header to your api routes and also throw exceptions as JSON.- A list of macros that extend the Laravel's Response class, that can be easily used like
response()->success(...)
_ide_helper_macros.php
file generated to improve your IDE's code completion
1️⃣ Installation
- You can install the package via composer:
composer require igorsgm/laravel-api-responses
- Publishing the config and IDE Helper files:
php artisan vendor:publish --provider="Igorsgm\LaravelApiResponses\LaravelApiResponsesServiceProvider"
2️⃣ Usage
First, register the ApiResponse
at the top of your api middleware group, inside app/Http/Kernel.php
.
class Kernel extends HttpKernel { protected $middlewareGroups = [ // ... 'api' => [ \Igorsgm\LaravelApiResponses\Http\Middleware\ApiResponse::class, // ... ], ]; // ... }
Now simply start using one of the available response functions to always return a normalized API data inside your controllers.
response()->success($data, $message) // ($data = [], $message = '', $status = 200, $headers = []) response()->successMessage($message) // ($message = '', $status = 200, $headers = []) response()->error($errors) // ($errors = [], $message = '', $status = 500, $headers = []) response()->errorMessage($message) // ($message = '', $status = 500, $headers = []) response()->exceptionError($exception) // ($exception, $message = '', $status = 0, $headers = [])
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.