PHPackages                             pepperfm/api-responder-for-laravel - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. pepperfm/api-responder-for-laravel

ActiveLibrary[HTTP &amp; Networking](/categories/http)

pepperfm/api-responder-for-laravel
==================================

Easy api responder template using via DI

3.0.1(1mo ago)41.6k1MITPHPPHP ^8.2CI passing

Since Apr 28Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/pepperfm/api-responder-for-laravel)[ Packagist](https://packagist.org/packages/pepperfm/api-responder-for-laravel)[ Docs](https://github.com/pepperfm/api-responder-for-lravel)[ RSS](/packages/pepperfm-api-responder-for-laravel/feed)WikiDiscussions 3.x Synced 1mo ago

READMEChangelog (8)Dependencies (18)Versions (64)Used By (0)

Api responder for Laravel
=========================

[](#api-responder-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ba9f40769dbe2eedd5f117de4380892e3fc0ac7d6bd8087c238dba379f42263e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f706570706572666d2f6170692d726573706f6e6465722d666f722d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pepperfm/api-responder-for-laravel)[![Total Downloads](https://camo.githubusercontent.com/80549315c53bf0446f99cf25763875d2b167f7c98fe79906dfecef6a86c788a4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f706570706572666d2f6170692d726573706f6e6465722d666f722d6c61726176656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pepperfm/api-responder-for-laravel)[![GitHub Actions](https://github.com/pepperfm/api-responder-for-laravel/actions/workflows/main.yml/badge.svg)](https://github.com/pepperfm/api-responder-for-laravel/actions/workflows/main.yml/badge.svg)

[![logo](api_responder.jpg)](api_responder.jpg)

Standardized API JSON responses for Laravel
-------------------------------------------

[](#standardized-api-json-responses-for-laravel)

Tip

[Full Package Description](https://pepperfm.github.io/api-responder-for-laravel): Helpful advice for doing things better or more easily.

Installation
------------

[](#installation)

```
composer require pepperfm/api-responder-for-laravel
```

Usage
-----

[](#usage)

### Inject via Laravel DI

[](#inject-via-laravel-di)

```
use Pepperfm\ApiBaseResponder\Contracts\ResponseContract;

class UserController extends Controller
{
    public function __construct(public ResponseContract $json)
    {
    }
}
```

### Direct API (config-based data key)

[](#direct-api-config-based-data-key)

The simplest approach — data key and wrapping are resolved from config:

```
public function index(Request $request)
{
    $users = User::query()->whereIn('id', $request->input('ids'))->get();

    return $this->json->response($users->toArray());
}

public function store(StoreUserRequest $request)
{
    $user = User::create($request->validated());

    return $this->json->stored($user->toArray());
}

public function destroy(User $user)
{
    $user->delete();

    return $this->json->deleted();
}
```

### Builder API (explicit control)

[](#builder-api-explicit-control)

Use the fluent builder when you need per-response control over data keys or wrapping:

```
// Explicit data key
public function index()
{
    $users = User::all();

    return $this->json->withDataKey('users')->response($users->toArray());
}

// REST method convention (singular key for show/update, plural for index/etc.)
public function show(User $user)
{
    return $this->json->forMethod('show')->response($user->toArray());
}

// Disable wrapping (data spread into response root)
public function stats()
{
    return $this->json->build()->withoutWrapping()->response($stats);
}
```

### PHP Attributes (declarative style)

[](#php-attributes-declarative-style)

Use `#[ResponseDataKey]` and `#[WithoutWrapping]` attributes with `fromAction()`:

```
use Pepperfm\ApiBaseResponder\Attributes\ResponseDataKey;
use Pepperfm\ApiBaseResponder\Attributes\WithoutWrapping;

#[ResponseDataKey('user')]
public function show(User $user): JsonResponse
{
    return $this->json->fromAction()->response($user->toArray());
}

#[ResponseDataKey] // defaults to singular 'entity'
public function edit(User $user): JsonResponse
{
    return $this->json->fromAction()->response($user->toArray());
}

#[WithoutWrapping]
public function stats(): JsonResponse
{
    return $this->json->fromAction()->response($stats);
}
```

### Pagination

[](#pagination)

```
public function index(Request $request)
{
    $users = User::query()->paginate();

    return $this->json->paginated($users);
}

// With data mapping
public function index(Request $request)
{
    $users = User::query()->paginate();
    $dtoCollection = $users->getCollection()->mapInto(UserDto::class);

    return $this->json->paginated($dtoCollection->toArray(), $users);
}
```

### Error responses

[](#error-responses)

```
return $this->json->error('Not found', 404);
return $this->json->error('Validation failed', 422, $validator->errors());
```

### Facades

[](#facades)

```
use Pepperfm\ApiBaseResponder\Facades\BaseResponse;

return BaseResponse::response($data);
return BaseResponse::forMethod('index')->response($data);
return BaseResponse::fromAction()->response($data);
```

### Method injection / resolve

[](#method-injection--resolve)

```
public function index(Request $request, ResponseContract $json)
{
    return $json->response($users->toArray());
}

// or
return resolve(ResponseContract::class)->response($users->toArray());
```

Paginated response format
-------------------------

[](#paginated-response-format)

The pagination meta is extracted automatically from `LengthAwarePaginator` or `CursorPaginator`:

```
export interface IPaginatedResponse {
    current_page: number
    per_page: number
    last_page: number
    data: T[]
    from: number
    to: number
    total: number
    prev_page_url?: any
    next_page_url: string
    links: IPaginatedResponseLinks[]
}

export interface IPaginatedResponseLinks {
    url?: any
    label: string
    active: boolean
}
```

Configuration
-------------

[](#configuration)

Publish the config file:

```
php artisan vendor:publish --provider="Pepperfm\ApiBaseResponder\Providers\ApiBaseResponderServiceProvider" --tag="config"
```

Key options:

- `plural_data_key` — data key for collections (default: `'entities'`)
- `singular_data_key` — data key for single items (default: `'entity'`)
- `without_wrapping` — disable data-key wrapping globally (default: `false`)
- `using_for_rest` — enable REST method-based key resolution (default: `true`)
- `methods_for_singular_key` — methods that use singular key (default: `['show', 'update']`)
- `force_json_response_header` — auto-add `Accept: application/json` to API requests (default: `true`)

### Check the configuration file to customize response wrapping as you prefer

[](#check-the-configuration-file-to-customize-response-wrapping-as-you-prefer)

---

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

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

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Security

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Dmitry Gaponenko](https://github.com/pepperfm)
- [Website](https://pepperfm.ru)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance89

Actively maintained with recent releases

Popularity24

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 99.2% 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

Every ~17 days

Recently: every ~74 days

Total

63

Last Release

57d ago

Major Versions

1.2.7 → 2.02024-03-13

2.x-dev → 3.0.02026-03-16

PHP version history (2 changes)1.0.0PHP ^8.2

1.2.7PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/87b33c1f84ded521a768afdbd204f1a1c2cd7519477a2f8bdb2e8b7cba030d09?d=identicon)[PepperFM](/maintainers/PepperFM)

---

Top Contributors

[![pepperfm](https://avatars.githubusercontent.com/u/36007880?v=4)](https://github.com/pepperfm "pepperfm (127 commits)")[![tabuna](https://avatars.githubusercontent.com/u/5102591?v=4)](https://github.com/tabuna "tabuna (1 commits)")

---

Tags

responsejsonapilaravelrestpepperfmapi-responder

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/pepperfm-api-responder-for-laravel/health.svg)

```
[![Health](https://phpackages.com/badges/pepperfm-api-responder-for-laravel/health.svg)](https://phpackages.com/packages/pepperfm-api-responder-for-laravel)
```

###  Alternatives

[guanguans/laravel-api-response

Normalize and standardize Laravel API response data structure. - 规范化和标准化 Laravel API 响应数据结构。

485.6k](/packages/guanguans-laravel-api-response)[dreamfactory/df-core

DreamFactory(tm) Core Components

1651.7k20](/packages/dreamfactory-df-core)[rap2hpoutre/jacky

Opinionated REST JSON HTTP API client for laravel

174.4k](/packages/rap2hpoutre-jacky)[ory/hydra-client-php

Documentation for all of Ory Hydra's APIs.

1710.8k](/packages/ory-hydra-client-php)

PHPackages © 2026

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