PHPackages                             kduma/content-negotiable-responses - 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. kduma/content-negotiable-responses

ActiveLibrary[API Development](/categories/api)

kduma/content-negotiable-responses
==================================

Laravel helpers for creating content-type negotiable responses

v2.0.0(1mo ago)134MITPHPPHP ^8.3

Since May 21Pushed 8mo ago1 watchersCompare

[ Source](https://github.com/kduma-OSS/LV-content-negotiable-responses)[ Packagist](https://packagist.org/packages/kduma/content-negotiable-responses)[ RSS](/packages/kduma-content-negotiable-responses/feed)WikiDiscussions master Synced today

READMEChangelog (2)Dependencies (10)Versions (5)Used By (0)

Check full documentation here: [opensource.duma.sh/libraries/php/laravel-content-negotiable-responses](https://opensource.duma.sh/libraries/php/laravel-content-negotiable-responses)

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

[](#installation)

```
composer require kduma/content-negotiable-responses
```

Content Formats (for `ArrayResponse` and `BasicViewResponse`)
-------------------------------------------------------------

[](#content-formats-for-arrayresponse-and-basicviewresponse)

Currently supported formats are:

- `text/plain` (disabled by default, to enable it in your custom class implement `TextResponseInterface`) - resulting response will be output of built-in PHP function `print_r`
- `application/json` - resulting response will be output of built-in PHP function `json_encode`
- `application/yaml` - resulting response will be generated using [symfony/yaml](https://packagist.org/packages/symfony/yaml) package
- `application/xml` - resulting response will be generated using [spatie/array-to-xml](https://packagist.org/packages/spatie/array-to-xml) package
- `application/msgpack` - resulting response will be generated using [rybakit/msgpack](https://packagist.org/packages/rybakit/msgpack) package
- `text/html` (only for `BasicViewResponse`) - resulting response will be generated using view provided to constructor with passed data array

Usage
-----

[](#usage)

### Basic Array Usage (for API responses)

[](#basic-array-usage-for-api-responses)

```
Route::get('/test', function () {
    return new \KDuma\ContentNegotiableResponses\ArrayResponse([
        'success' => true,
        'timestamp' => time(),
    ]);
});
```

As the result, response will be formatted accordingly to acceptable formats passed in `Accept` HTTP header or as JSON if not specified.

### Basic View Usage (for Web and API responses)

[](#basic-view-usage-for-web-and-api-responses)

```
Route::get('/', function () {
    return new \KDuma\ContentNegotiableResponses\BasicViewResponse('welcome', [
        'success' => true,
        'timestamp' => time(),
    ]);
});
```

### Customized Usage

[](#customized-usage)

```
namespace App\Http\Responses;

use KDuma\ContentNegotiableResponses\BaseViewResponse;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

class UsersListResponse extends BaseViewResponse
{
    public $users;

    public function __construct(Collection $users)
    {
        $this->users = $users;
    }

    protected function generateView(Request $request)
    {
        return $this->view('users.list');
    }
}

Route::get('/users', function () {
    return new \App\Http\Responses\UsersListResponse(\App\User::all());
});
```

As the result, when opening in web browser, there will be rendered `users.list` view with passed all public properties to it. In non HTML clients (specyfing other `Accept` headers) will get serialized public properties (in any supported format), for example:

```
{
    "users": [
        {
            "name": "user 1",
            "email": "user1@localhost"
        },
        {
            "name": "user 2",
            "email": "user2@localhost"
        },
        {
            "name": "user 3",
            "email": "user3@localhost"
        }
    ]
}
```

If you want to customize serialized array, you need to override `getDataArray` method:

```
/**
 * @return array
 */
protected function getDataArray() {
	return [
		'my_super_users_collection' => $this->users->toArray(),
		'hello' => true
	];
}
```

Then the result will be:

```
{
    "my_super_users_collection": [
        {
            "name": "user 1",
            "email": "user1@localhost"
        },
        {
            "name": "user 2",
            "email": "user2@localhost"
        },
        {
            "name": "user 3",
            "email": "user3@localhost"
        }
    ],
    "hello": true
}
```

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance75

Regular maintenance activity

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity77

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~1257 days

Total

3

Last Release

34d ago

Major Versions

0.1 → 1.02022-10-20

1.0 → v2.0.02026-04-08

PHP version history (2 changes)1.0PHP ^8.1

v2.0.0PHP ^8.3

### Community

Maintainers

![](https://www.gravatar.com/avatar/d09451622719c7efce1a7971b8e5a875859504882ba2b8c279d1015a99e8f12c?d=identicon)[kduma](/maintainers/kduma)

---

Top Contributors

[![kduma](https://avatars.githubusercontent.com/u/1062582?v=4)](https://github.com/kduma "kduma (9 commits)")

---

Tags

laravelphp

### Embed Badge

![Health badge](/badges/kduma-content-negotiable-responses/health.svg)

```
[![Health](https://phpackages.com/badges/kduma-content-negotiable-responses/health.svg)](https://phpackages.com/packages/kduma-content-negotiable-responses)
```

###  Alternatives

[spatie/laravel-query-builder

Easily build Eloquent queries from API requests

4.4k26.9M220](/packages/spatie-laravel-query-builder)[essa/api-tool-kit

set of tools to build an api with laravel

52680.5k](/packages/essa-api-tool-kit)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

44643.1k1](/packages/pressbooks-pressbooks)[simplestats-io/laravel-client

Client for SimpleStats!

4515.5k](/packages/simplestats-io-laravel-client)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

255.2k](/packages/aedart-athenaeum)[bjuppa/laravel-blog

Add blog functionality to your Laravel project

483.3k2](/packages/bjuppa-laravel-blog)

PHPackages © 2026

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