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.
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.
GET /api/v1/users
Accept: application/xml
<?xml version="1.0" encoding="utf-8"?>
<xml>
<item>
<id>30</id>
<name>Guilherme</name>
<surname>Girardi</surname>
<email>guilhermeagirardi@gmail.com</email>
<created_at>2019-04-24 20:34:03</created_at>
<updated_at>2019-04-24 20:34:03</updated_at>
</item>
...
</xml>
GET /api/v1/users
Accept: application/json
[
{
"id": 30,
"name":"Guilherme",
"surname": "Girardi",
"email": "guilhermeagirardi@gmail.com",
"created_at": "2019-04-24 20:34:03",
"updated_at": "2019-04-24 20:34:03"
}
]
GET /api/v1/users
Accept: text/csv
id,name,surname,email,created_at,updated_at
30,Guilherme,Girardi,guilhermeagirardi@gmail.com,"2019-04-24 20:34:03","2019-04-24 20:34:03"
GET /api/v1/users
Accept: application/x-yaml
id: 30
name: Guilherme
surname: Girardi
email: guilhermeagirardi@gmail.com
created_at: '2019-04-24 20:34:03'
updated_at: '2019-04-24 20:34:03'
- 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()
;
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:
Route::middleware(['cors', 'api-response-formatter'])->prefix('/v1')->name('api.')->group(function() {
Route::prefix('users')->name('users.')->group(function () {
Route::get('me', 'UsersController@me')->name('me');
});
});
Nothing for now...
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! π
This library is a open-source software licensed under the MIT license.