PHP code example of jdesrosiers / silex-conneg-provider

1. Go to this page and download the library: Download jdesrosiers/silex-conneg-provider library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jdesrosiers / silex-conneg-provider example snippets


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

$request->getRequestFormat($defaultFormat);

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