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

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

rszrama/negotiation-middleware
==============================

Content negotiation middleware for PHP applications using a request, response, and next callable signature.

412.2k6[3 issues](https://github.com/rszrama/negotiation-middleware/issues)PHP

Since Jan 24Pushed 10y ago3 watchersCompare

[ Source](https://github.com/rszrama/negotiation-middleware)[ Packagist](https://packagist.org/packages/rszrama/negotiation-middleware)[ RSS](/packages/rszrama-negotiation-middleware/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

NegotiationMiddleware
=====================

[](#negotiationmiddleware)

NegotiationMiddleware provides content negotiation middleware for PHP applications using a middleware signature that requires a request object, a response object, and the next callable in the middleware stack.

This library depends on [willdurand/negotation](https://github.com/willdurand/Negotiation) for content negotiation. It allows you to add negotiation to a middleware stack that:

1. Identifies and matches against a list of acceptable media types.
2. Supplies a default media type in the absence of an Accept request header.
3. Enriches the request object with the negotiated media type.

If the negotiator cannot determine which media type to use in response to the request, it will return a response with a 406 Not Acceptable status.

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

[](#installation)

Use [Composer](https://getcomposer.org/) to install NegotiationMiddleware:

```
$ composer require rszrama/negotiation-middleware
```

This will install the library and its dependencies. NegotiationMiddleware requires PHP 5.4.0 or newer.

Usage
-----

[](#usage)

Add an instance of NegotiationMiddleware\\Negotiator to an application or route level middleware stack, passing two arguments to the constructor: an array of acceptable media types to be matched against and a boolean indicating whether or not the middleware should simply match the first acceptable media type in the absence of an Accept header in the request.

Example from Slim 3.x:

```
