ejetar / laravel-api-response-formatter
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.
Requires
- laravel/framework: ^7.17
- symfony/serializer: ^5.1
- symfony/yaml: ^5.1
Requires (Dev)
- orchestra/testbench: ~3.4|~3.5|~3.6|~3.7|~3.8|~4.0|~5.0
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ~5.0
This package is auto-updated.
Last update: 2024-12-30 02:12:42 UTC
README
Table of Contents
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
GET /api/v1/users
Accept: application/xml
Output
<?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>
Example 2
GET /api/v1/users
Accept: application/json
Output
[ { "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" } ]
Example 3
GET /api/v1/users
Accept: text/csv
Output
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"
Example 4
GET /api/v1/users
Accept: application/x-yaml
Output
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'
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:
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'); }); });
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.