Download the PHP package ejetar/laravel-api-response-formatter without Composer
On this page you can find all versions of the php package ejetar/laravel-api-response-formatter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ejetar/laravel-api-response-formatter
More information about ejetar/laravel-api-response-formatter
Files in ejetar/laravel-api-response-formatter
Package laravel-api-response-formatter
Short Description A simple and fast 🚀 library that displays responses in various formats, according to the Accept header, entered by the user. The library currently displays responses in JSON, XML, CSV, and YAML.
License MIT
Homepage https://ejetar.com/projects/api-response-formatter
Informations about the package laravel-api-response-formatter
API Response Formatter
Table of Contents
- About
- How it works
- Example 1
- Output
- Example 2
- Output
- Example 3
- Output
- Example 4
- Output
- Installation
- Get started
- Changelog
- Contributing
- License
About
A simple and fast 🚀 library that displays responses in various formats, according to the Accept header, entered by the user. The library currently displays responses in JSON, XML, CSV, and YAML.
How it works
The API Response Formatter package provides the developer with a middleware called api-response-formatter. This middleware takes Response and converts it to a certain format based on the user's request.
In practical terms:
- Reads the contents of the Accept header to know which response format to display to the user.
- Takes the original response content provided by Laravel and convert it to the desired format;
- Displays the response to the user;
It also provides a custom ExceptionHandler, which allows exceptions to also be thrown in the formats you want. A very nice trick of this handler is that when the route is /api/*, it forces the response to be in JSON, CSV, YAML or XML (if no type is informed via Accept, then JSON is selected by default). This prevents the API, at some point of failure, returns an HTML error.
Example 1
Output
Example 2
Output
Example 3
Output
Example 4
Output
Installation
- First, run:
composer require ejetar/laravel-api-response-formatter
; - Add
Ejetar\ApiResponseFormatter\Providers\AppServiceProvider::class
inproviders
array inconfig/app.php
file; - In
app/Exceptions/Handler.php
, replaceIlluminate\Foundation\Exceptions\Handler
withEjetar\ApiResponseFormatter\Exceptions\Handler
; - In
public/index.php
, replaceIlluminate\Http\Request::capture()
withEjetar\ApiResponseFormatter\Requests\Request::capture()
;
Get started
To start using this package is very simple, just call Middlware api-response-formatter
in your routes/api.php
file.
As in the following example:
Changelog
Nothing for now...
Contributing
Contribute to this wonderful project, it will be a pleasure to have you with us. Let's help the free software community. You are invited to incorporate new features, make corrections, report bugs, and any other form of support. Don't forget to star in this repository! 😀
License
This library is a open-source software licensed under the MIT license.