2017 © Pedro Peláez
 

library silex-conneg-provider

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

image

jdesrosiers/silex-conneg-provider

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

  • Monday, June 6, 2016
  • by jdesrosiers
  • Repository
  • 2 Watchers
  • 6 Stars
  • 31,345 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 5 Versions
  • 4 % Grown

The README.md

silex-conneg-provider

Build Status Scrutinizer Code Quality Code Coverage, (*1)

The silex-conneg-provider is a silex service provider that provides tools for doing HTTP Content Negotiation 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 or Symfony Serializer., (*2)

Installation

Install the silex-conneg-provider using composer. This project uses sematic versioning., (*3)

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

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"), (*4)

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

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

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

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., (*6)

$request->getRequestFormat($defaultFormat);

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 or the Symfony Serializer accessible at $app["serializer"]. You can get the JMS Serializer from the jdesrosiers/silex-jms-serializer-provider or the Symfony Serializer from the silex built-in SerializerServiceProvider., (*7)

$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",
    ));
});

The Versions

06/06 2016

v0.x-dev

0.9999999.9999999.9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jason Desrosiers

silex http service provider content negotiation conneg

06/06 2016

dev-master

9999999-dev

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jason Desrosiers

silex http service provider content negotiation conneg

06/06 2016

v1.0.0

1.0.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jason Desrosiers

silex http service provider content negotiation conneg

19/02 2014

v0.1.1

0.1.1.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jason Desrosiers

silex http service provider content negotiation conneg

07/11 2013

v0.1.0

0.1.0.0

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

  Sources   Download

MIT

The Requires

 

The Development Requires

by Jason Desrosiers

silex http service provider content negotiation conneg