Exemple #1
0
$contentNegotiation = function () use($app) {
    $negotiator = new Negotiator();
    $format = $negotiator->getBest($app->request()->headers('Accept'));
    $type = $format->getValue();
    $app->contentType($type);
    $app->template = 'contacts/show.json.twig';
    if ('application/xml' === $type) {
        $app->template = 'contacts/show.xml.twig';
    }
};
$app->post('/contacts', $contentNegotiation, function () use($app) {
    $contactInformation = $app->request()->getBody();
    parse_str($contactInformation, $contact);
    $contact['contact_id'] = $app->faker->randomDigit;
    //Simulate an insert
    $app->status(201);
    echo $app->render($app->template, ['contact' => $contact]);
});
$app->get('/contacts/:id', $contentNegotiation, function ($id) use($app) {
    //This value should be specific to the current resource
    $lastModifiedTime = gmdate('D, d M Y H:i:s ') . ' GMT';
    $app->response()->header('Last-Modified', $lastModifiedTime);
    $app->status(200);
    echo $app->render($app->template, ['contact' => ['contact_id' => $id, 'name' => $app->faker->firstName, 'last_name' => $app->faker->lastName]]);
});
$app->put('/contacts/:id', $contentNegotiation, function ($id) use($app) {
    $contactInformation = $app->request()->getBody();
    parse_str($contactInformation, $contact);
    $contact['contact_id'] = $id;
    if (empty($contact['name'])) {
        $contact['name'] = $app->faker->firstName;