PHPackages                             lcobucci/content-negotiation-middleware - 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. lcobucci/content-negotiation-middleware

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

lcobucci/content-negotiation-middleware
=======================================

A PSR-15 middleware to handle content negotiation

3.2.0(2y ago)47266.3k↓33.8%2[1 issues](https://github.com/lcobucci/content-negotiation-middleware/issues)[4 PRs](https://github.com/lcobucci/content-negotiation-middleware/pulls)3MITPHPPHP ~8.1.0 || ~8.2.0 || ~8.3.0CI passing

Since Mar 27Pushed 1mo ago4 watchersCompare

[ Source](https://github.com/lcobucci/content-negotiation-middleware)[ Packagist](https://packagist.org/packages/lcobucci/content-negotiation-middleware)[ GitHub Sponsors](https://github.com/lcobucci)[ Patreon](https://www.patreon.com/lcobucci)[ RSS](/packages/lcobucci-content-negotiation-middleware/feed)WikiDiscussions 3.3.x Synced 1mo ago

READMEChangelog (10)Dependencies (17)Versions (21)Used By (3)

Content negotiation middleware
==============================

[](#content-negotiation-middleware)

[![Total Downloads](https://camo.githubusercontent.com/5b37882e7e72730811b3641faa3531225bfcfe3bfa1e7a94301f1ec98675e915/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c636f62756363692f636f6e74656e742d6e65676f74696174696f6e2d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lcobucci/content-negotiation-middleware)[![Latest Stable Version](https://camo.githubusercontent.com/0630f35aee4ef564623405d1a9ab893558683b20e6b55089fc72ae22f21ebcb7/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c636f62756363692f636f6e74656e742d6e65676f74696174696f6e2d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lcobucci/content-negotiation-middleware)[![Unstable Version](https://camo.githubusercontent.com/39bd350f6226939683ba796d5a16aa145eea3dc039ca0dabf7ba0d6737cb1df1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6c636f62756363692f636f6e74656e742d6e65676f74696174696f6e2d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lcobucci/content-negotiation-middleware)

[![Build Status](https://camo.githubusercontent.com/4ef803eac4e1c6311f1576b6e626e5c612b61c971ff72b4600faa0203f6e13d2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6c636f62756363692f636f6e74656e742d6e65676f74696174696f6e2d6d6964646c65776172652f706870756e69742e796d6c3f7374796c653d666c61742d737175617265)](https://github.com/lcobucci/content-negotiation-middleware/actions?query=workflow%3A%22PHPUnit%20Tests%22+branch%3A3.1)[![Code Coverage](https://camo.githubusercontent.com/d6597582f8d60687f5d57fdde4d27e00a483d28fd82dae467df953c0c0e37fe8/68747470733a2f2f636f6465636f762e696f2f67682f6c636f62756363692f636f6e74656e742d6e65676f74696174696f6e2d6d6964646c65776172652f6272616e63682f332e312f67726170682f62616467652e737667)](https://codecov.io/gh/lcobucci/content-negotiation-middleware)

Motivation
----------

[](#motivation)

Packages like `middlewares/negotiation` do a very good job to detect the correct content type based on the `Accept` header (or extension in the URI), however they delegate to the `RequestHandler` to format the content according to the detected mime type.

That works fine for most cases but it usually creates a lot of duplication in complex software, where every single `RequestHandler` should do that formatting (or depend on some component to do that). That logic should also be added to the middleware that handles exceptions and converts them to the appropriated HTTP response.

The goal of this middleware is to provide full content negotiation (detection and formatting).

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

[](#installation)

This package is available on [Packagist](http://packagist.org/packages/lcobucci/content-negotiation-middleware), and we recommend you to install it using [Composer](http://getcomposer.org):

```
composer require lcobucci/content-negotiation-middleware middlewares/negotiation laminas/laminas-diactoros
```

### Adventure mode

[](#adventure-mode)

If you're ready for an adventure and don't want to use `middlewares/negotiation`to handle the detection or `laminas/diactoros` to create the response body (`StreamInterface` implementation), don't despair! You'll only have to use the normal `ContentTypeMiddleware::__construct()` instead of `ContentTypeMiddleware::fromRecommendedSettings()`.

We do have a small preference for the mentioned packages and didn't want to reinvent the wheel... but you know, it's a free world.

PHP Configuration
-----------------

[](#php-configuration)

In order to make sure that other components are returning the expected objects we decided to use `assert()`, which is a very interesting feature in PHP but not often used. The nice thing about `assert()` is that we can (and should) disable it in production mode so that we don't have useless statements.

So, for production mode, we recommend you to set `zend.assertions` to `-1` in your `php.ini`. For development, you should leave `zend.assertions` as `1` and set `assert.exception` to `1`, which will make PHP throw an [`AssertionError`](https://secure.php.net/manual/en/class.assertionerror.php)when things go wrong.

Check the documentation for more information:

Usage
-----

[](#usage)

Your very first step is to create the middleware using the correct configuration:

```
