gaurav93d / laravelresponder
A tiny Laravel package to format API responses.
1.0.0
2019-03-09 10:12 UTC
Requires
Requires (Dev)
- mockery/mockery: ^1.1
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~7.0
- sempro/phpunit-pretty-print: ^1.0
This package is not auto-updated.
Last update: 2025-04-01 15:36:30 UTC
README
A tiny Laravel package to format API responses.
Installation
Via Composer
$ composer require gaurav93d/laravelresponder
For Laravel version < 5.5, consider listing service provider and facade in config/app.php
'providers' => [ ... gaurav93d\LaravelResponder\LaravelResponderServiceProvider::class, ... ], 'aliases' => [ ... 'Responder' => gaurav93d\LaravelResponder\Facades\Responder::class, ... ],
Usage
Response format
{ "success": true, "status": 200, "data": [ "Here your beautiful data goes" ], "errors": [ "Here your ugly errors go" ] }
Send success
... use gaurav93d\LaravelResponder\Facades\Responder; ... Responder::success($data = [], $status = 200);
Send errors
... use gaurav93d\LaravelResponder\Facades\Responder; ... // Multiple errors return Responder::errors($errors = [], $status = 200); // Single error return Responder::error($message = 'Error!', $status = 200); // Validation errors return Responder::respondValidationErrors(Validator $validator); // Here are some comman error responses 👇 // Internal server error return Responder::respondInternalError($message = 'Internal Error!'); // status = 500 // Unauthorized error return Responder::respondUnauthorizedError($message = 'Unauthorized!'); // status = 401 // Bad request error return Responder::respondBadRequestError($message = 'Bad Request!'); // status = 400 // Not found error return Responder::respondNotFoundError($message = 'Not found!'); // status = 404
Attach response headers
... use gaurav93d\LaravelResponder\Facades\Responder; ... return Responder::headers($headers = [])->success($data = [], $status = 200); ... return Responder::headers($headers = [])->errors($errors = [], $status = 200); ...
... 😉 Stay tuned! More features to come.
Change log
Please see the changelog for more information on what has changed recently.
Credits
License
MIT. Please see the license file for more information.