Download the PHP package tequilarapido/api-response without Composer
On this page you can find all versions of the php package tequilarapido/api-response. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tequilarapido/api-response
More information about tequilarapido/api-response
Files in tequilarapido/api-response
Package api-response
Short Description A simple way to return well formatted json responses in a Laravel application.
License MIT
Homepage https://github.com/tequilarapido/api-response
Informations about the package api-response
A simple way to return well formatted json responses in a Laravel application.
Installation
You can install the package using composer
Add the service provider
Usage
This package comes with a helper function api_response(), as sugar syntax to using app(Tequilarapido\ApiResponse\ApiResponse::class)
Return a response from an item (Array / Associative array / Object/)
Result :
Return a response from a collection
Result :
Return a response from a paginated collection
Result :
Transformers
You can use transformers along with item(), collection(), and paginatedCollection methods to transform results
before returning the responses.
To learn more about the concept behind transformers, please read the League\Fractal documentation.
Put in a simple way, a Transformer class
- must extends
League\Fractal\TransformerAbstract - and contains a method
transform()that take the raw value/object as an argument and transform it the way we want.
Let's assume that we need to return an api response containing a collection of books retreived from database, and we want to format/enhance/restrict results.
Result:
Attaching cookies
For item(), collection(), and paginatedCollection methods the returned result is a built Illuminate\Http\Response object.
To be able to attach cookies you need to instruct api_response() methods to not build the response by setting the $built argument to false, attach the cookie
and then build the response.
Attaching headers
For item(), collection(), and paginatedCollection methods the returned result is a built Illuminate\Http\Response object.
To be able to attach headers you need to instruct api_response() methods to not build the response by setting the $built argument to false, attach the header
and then build the response.
Other useful methods
| Method | Usage |
|---|---|
api_response()->noContent() |
Return an empty response with 204 No Content header. |
api_response()->errorNotFound() |
Return a 404 Not found. |
api_response()->errorBadRequest() |
Return a 404 Bad Request. |
api_response()->errorForbidden() |
Return a 403 Forbidden. |
api_response()->errorUnAUthorized() |
Return a 401 Unauthorized. |
api_response()->errorInternal() |
Return a 500 Internal Error. |
api_response()->error() |
Return a more customizable error (Accept MessageBag instance, staus code ...) |
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
Security
If you discover any security related issues, please email :author_email instead of using the issue tracker.
Contributing
Please see CONTRIBUTING for details.
Credits
- Nassif Bourguig
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of api-response with dependencies
laravel/framework Version ~5.8.0|^6.0|^7.0
league/fractal Version ^0.14.0