PHPackages                             ejetar/laravel-api-response-formatter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. ejetar/laravel-api-response-formatter

ActiveLibrary[API Development](/categories/api)

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.

1.0.0(5y ago)791MITPHPCI failing

Since Jul 3Pushed 5y ago2 watchersCompare

[ Source](https://github.com/ejetar/laravel-api-response-formatter)[ Packagist](https://packagist.org/packages/ejetar/laravel-api-response-formatter)[ Docs](https://ejetar.com/projects/api-response-formatter)[ RSS](/packages/ejetar-laravel-api-response-formatter/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

API Response Formatter
======================

[](#api-response-formatter)

Table of Contents
-----------------

[](#table-of-contents)

- [About](#about)
- [How it works](#how-it-works)
    - [Example 1](#example-1)
        - [Output](#output)
    - [Example 2](#example-2)
        - [Output](#output-1)
    - [Example 3](#example-3)
        - [Output](#output-2)
    - [Example 4](#example-4)
        - [Output](#output-3)
- [Installation](#installation)
- [Get started](#get-started)
- [Changelog](#changelog)
- [Contributing](#contributing)
- [License](#license)

About
-----

[](#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
------------

[](#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:

1. Reads the contents of the Accept header to know which response format to display to the user.
2. Takes the original response content provided by Laravel and convert it to the desired format;
3. 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

[](#example-1)

```
GET /api/v1/users
Accept: application/xml

```

#### Output

[](#output)

```

        30
        Guilherme
        Girardi
        guilhermeagirardi@gmail.com
        2019-04-24 20:34:03
        2019-04-24 20:34:03

    ...

```

### Example 2

[](#example-2)

```
GET /api/v1/users
Accept: application/json

```

#### Output

[](#output-1)

```
[
    {
        "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

[](#example-3)

```
GET /api/v1/users
Accept: text/csv

```

#### Output

[](#output-2)

```
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

[](#example-4)

```
GET /api/v1/users
Accept: application/x-yaml

```

#### Output

[](#output-3)

```
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
------------

[](#installation)

1. First, run: `composer require ejetar/laravel-api-response-formatter`;
2. Add `Ejetar\ApiResponseFormatter\Providers\AppServiceProvider::class` in `providers` array in `config/app.php` file;
3. In `app/Exceptions/Handler.php`, replace `Illuminate\Foundation\Exceptions\Handler` with `Ejetar\ApiResponseFormatter\Exceptions\Handler`;
4. In `public/index.php`, replace `Illuminate\Http\Request::capture()` with `Ejetar\ApiResponseFormatter\Requests\Request::capture()`;

Get started
-----------

[](#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
---------

[](#changelog)

Nothing for now...

Contributing
------------

[](#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
-------

[](#license)

This library is a open-source software licensed under the MIT license.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 92.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2137d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/781358602863d7536e95494388bda4d9f53175fdf5029d9db7b45d4eabef10e9?d=identicon)[ejetar](/maintainers/ejetar)

---

Top Contributors

[![ggirardi1](https://avatars.githubusercontent.com/u/2306117?v=4)](https://github.com/ggirardi1 "ggirardi1 (13 commits)")[![imsus](https://avatars.githubusercontent.com/u/1058471?v=4)](https://github.com/imsus "imsus (1 commits)")

---

Tags

accept-headerapiconvertcsvdataformatformatterjsonlaraveloutputphpresponsexmlyamlresponsephpjsonformatterapilaravelxmlconvertdatayamlcsvformatoutputaccept header

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ejetar-laravel-api-response-formatter/health.svg)

```
[![Health](https://phpackages.com/badges/ejetar-laravel-api-response-formatter/health.svg)](https://phpackages.com/packages/ejetar-laravel-api-response-formatter)
```

###  Alternatives

[soapbox/laravel-formatter

A formatting library that converts data output between XML, CSV, JSON, TXT, YAML and a few others.

2501.1M12](/packages/soapbox-laravel-formatter)[hitsend/laravel-formatter

A formatting library that converts data output between XML, CSV, JSON, TXT, YAML and a few others.

2502.0k](/packages/hitsend-laravel-formatter)[obiefy/api-response

Simple Laravel package to return Json responses.

17324.6k](/packages/obiefy-api-response)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
