dafiti/contentnegotiation-service-provider

A Silex Service Provider for Simple Content Negotiation

0.0.2 2015-05-12 01:11 UTC

This package is not auto-updated.

Last update: 2021-06-12 00:10:10 UTC


README

Build Status Scrutinizer Code Quality Code Coverage HHVM Latest Stable Version Total Downloads License

A Silex Service Provider for Simple Content Negotiation.

Instalation

The package is available on Packagist. Autoloading is PSR-4 compatible.

{
    "require": {
        "dafiti/contentnegotiation-service-provider": "dev-master"
    }
}

Usage

use Silex\Application;
use Dafiti\Silex\ContentNegotiationServiceProvider;

$config = [    
    'available_accepts' => [
        'application/json',
        'application/xml'
    ],
    'default_accept'    => 'application/json'
];

$app = new Application();
$app->register(new ContentNegotiationServiceProvider($config));

$app->get("/your-endpoint", function() {
    $data = ["you data to response"];
    
    return new \Dafiti\Silex\Response($data);
});

Request Examples:

Request

HTTP GET
Header: Accept: application/json
URL: http://baseurl.com/your-endpoint

####Response

Response Header: Content-Type: application/json
Status Code: 200
Body:
{
    "you data to response"
}

####Request

HTTP GET
Header: Accept: text/html
URL: http://baseurl.com/your-endpoint

Response

Response Header: Content-Type: application/json
Status Code: 406
Body:
{
    "message":"Accept Type Not Acceptable"
}

License

MIT License