PHPackages                             jdesrosiers/silex-conneg-provider - 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. jdesrosiers/silex-conneg-provider

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

jdesrosiers/silex-conneg-provider
=================================

A silex service provider that provides tools for doing HTTP content negotiation.

v1.0.0(10y ago)653.2k↓91.7%1[1 PRs](https://github.com/jdesrosiers/silex-conneg-provider/pulls)1MITPHPCI failing

Since Nov 7Pushed 6y ago2 watchersCompare

[ Source](https://github.com/jdesrosiers/silex-conneg-provider)[ Packagist](https://packagist.org/packages/jdesrosiers/silex-conneg-provider)[ RSS](/packages/jdesrosiers-silex-conneg-provider/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (3)Dependencies (5)Versions (5)Used By (1)

silex-conneg-provider
=====================

[](#silex-conneg-provider)

[![Build Status](https://camo.githubusercontent.com/038a84845fdb40d164618f58bd5a281288075983ffc9f34561bc836ca795b423/68747470733a2f2f7472617669732d63692e6f72672f6a646573726f73696572732f73696c65782d636f6e6e65672d70726f76696465722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/jdesrosiers/silex-conneg-provider)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/3f6a0a0e4205fc2fbecfa48db096ce6bad8f73d485f5110ccb237bbab14ad38b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a646573726f73696572732f73696c65782d636f6e6e65672d70726f76696465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jdesrosiers/silex-conneg-provider/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/964254eeaa82d8a3b5003a5f83e055f4b746a7907ba4ed3cc813446af4040049/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a646573726f73696572732f73696c65782d636f6e6e65672d70726f76696465722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jdesrosiers/silex-conneg-provider/?branch=master)

The silex-conneg-provider is a [silex](https://github.com/fabpot/Silex) service provider that provides tools for doing [HTTP Content Negotiation](http://www.w3.org/Protocols/rfc2616/rfc2616-sec12.html) in your application. It allows you to declare which request and response formats your application can handle. If the client requests a response in a format your application does not support, they will get a `406 Not Acceptable` response. If the client sends a request body in a format your application does not support, they will get a `415 Unsupported Media Type` response. There is also a service to make it easy to automatically serialize responses and deserialize requests using [JMS Serialzier](http://jmsyst.com/libs/serializer) or [Symfony Serializer](http://symfony.com/doc/current/components/serializer.html).

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

[](#installation)

Install the silex-conneg-provider using [composer](http://getcomposer.org/). This project uses [sematic versioning](http://semver.org/).

```
{
    "require": {
        "jdesrosiers/silex-conneg-provider": "~1.0"
    }
}
```

Parameters
----------

[](#parameters)

Content Negotiation

- **conneg.responseFormats**: (array) Array of supported response formats. Defaults to `array("html")`
- **conneg.requestFormats**: (array) Array of supported request formats. Defaults to `array("form")`

Serialization

- **conneg.defaultFormat**: (string) Defaults to `html`
- **conneg.serializationContext**: (JMS\\Serializer\\SerializationContext or array). Optional.
- **conneg.deserializationContext**: (JMS\\Serializer\\DeserializationContext or array). Optional.

Services
--------

[](#services)

- **conneg**: Provides an object with two methods: createResponse and deserializeRequest. This service is only available if you have a serializer service installed.
- **createResponse**: This works just like `Respose::create`, but takes an object instead of a string and serializes it to the desired format. The format is determined by the middleware this service provider includes.
- **deserializeRequest**: Pass it a class name and it will deserialize the request entity and give you back an instance of that class.

Registering
-----------

[](#registering)

```
$app->register(new JDesrosiers\Silex\Provider\ContentNegotiationServiceProvider(), array(
    "conneg.responseFormats" => array("json", "xml"),
    "conneg.requestFormats" => array("json", "xml"),
    "conneg.defaultFormat" => "json",
));
```

Usage
-----

[](#usage)

The service provider adds middleware that does all of the content negotiation header validation automatically and responds appropriately when the request can not be handled. You can get the response format determined by the middleware using the `Request::getRequestFormat` method.

```
$request->getRequestFormat($defaultFormat);
```

Serailizer Usage
----------------

[](#serailizer-usage)

The `conneg` service provides some helper functions for automatically serailizing responses and deserializing requests. This functionality is available if you have an instance of either the [JMS Serialzier](http://jmsyst.com/libs/serializer)or the [Symfony Serializer](http://symfony.com/doc/current/components/serializer.html) accessible at `$app["serializer"]`. You can get the JMS Serializer from the [jdesrosiers/silex-jms-serializer-provider](https://github.com/jdesrosiers/silex-jms-serializer-provider) or the Symfony Serializer from the silex built-in `SerializerServiceProvider`.

```
$app->post("/foo", function (Request $request) use ($app) {
    // deserializeRequest takes the class name of a JMS Serializer annotated class and will deserialize
    // the request entity and give you back an instance of that class.
    $requestData = $app["conneg"]->deserializeRequest("FooRequest");

    $response = Foo::create($requestData);

    // createResponse works just like Respose::create, but takes a JMS Serializer annotated object
    // instead of a string and serializes it to the format the user requested.
    return $app["conneg"]->createResponse($response, 201, array(
        "Location" => "/foo/1234",
    ));
});
```

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity66

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 ~314 days

Total

4

Last Release

3677d ago

Major Versions

v0.1.1 → v1.0.02016-06-06

### Community

Maintainers

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

---

Top Contributors

[![jdesrosiers](https://avatars.githubusercontent.com/u/716571?v=4)](https://github.com/jdesrosiers "jdesrosiers (20 commits)")

---

Tags

httpservice providersilexcontent negotiationconneg

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jdesrosiers-silex-conneg-provider/health.svg)

```
[![Health](https://phpackages.com/badges/jdesrosiers-silex-conneg-provider/health.svg)](https://phpackages.com/packages/jdesrosiers-silex-conneg-provider)
```

###  Alternatives

[guzzlehttp/psr7

PSR-7 message implementation that also provides common utility methods

8.0k1.1B3.9k](/packages/guzzlehttp-psr7)[psr/http-message

Common interface for HTTP messages

7.0k1.1B6.7k](/packages/psr-http-message)[psr/http-factory

PSR-17: Common interfaces for PSR-7 HTTP message factories

1.9k747.1M2.5k](/packages/psr-http-factory)[psr/link

Common interfaces for HTTP links

2.5k152.8M84](/packages/psr-link)[league/uri

URI manipulation library

1.1k240.0M380](/packages/league-uri)[league/uri-interfaces

Common tools for parsing and resolving RFC3987/RFC3986 URI

539238.7M43](/packages/league-uri-interfaces)

PHPackages © 2026

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