/** * @SWG\Api( * path="/gas-stations/{gasStationId}", * @SWG\Operation( * method="OPTIONS", * summary="Get the valid options of a gas station", * nickname="optionsGasStation", * @SWG\Parameter( * name="gasStationId", * description="ID of gas station", * paramType="path", * required=true, * type="integer" * ), * @SWG\ResponseMessage( * code=404, * message="Gas station not found" * ) * ) * ) */ protected function optionsStation() { $this->app->options('/gas-stations/:id', function ($id) { $this->app->stationController->options(); }); }
$app->put('/:resource(/(:action)(/))', function ($resource, $subResource = null) use($app) { $resource = Resource::load($app->version, $resource, $subResource); if ($resource === null) { Resource::error(Resource::STATUS_NOT_FOUND, 'Cannot find requested resource.'); } else { $resource->put(); } }); // Delete $app->delete('/:resource(/(:action)(/))', function ($resource, $subResource = null) use($app) { $resource = Resource::load($app->version, $resource, $subResource); if ($resource === null) { Resource::error(Resource::STATUS_NOT_FOUND, 'Cannot find requested resource.'); } else { $resource->delete(); } }); // Options $app->options('/:resource(/(:action)(/))', function ($resource, $subResource = null) use($app) { $resource = Resource::load($app->version, $resource, $subResource); if ($resource === null) { Resource::error(Resource::STATUS_NOT_FOUND, 'Cannot find requested resource.'); } else { $resource->options(); } }); // Not found $app->notFound(function () { Resource::error(Resource::STATUS_NOT_FOUND, 'Cannot find requested resource.'); }); $app->run();
$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; } if (empty($contact['last_name'])) { $contact['last_name'] = $app->faker->lastName; } $lastModifiedTime = time(); $app->lastModified($lastModifiedTime); $app->status(200); echo $app->render($app->template, ['contact' => $contact]); }); $app->delete('/contacts/:id', function ($id) use($app) { //Delete contact here $app->status(204); }); $app->options('/contacts/:id', function ($id) use($app) { $validOptions = ['GET', 'HEAD', 'PUT', 'DELETE', 'OPTIONS']; $app->response()->header('Allow', implode(',', $validOptions)); }); $app->run();
<?php require 'vendor/autoload.php'; use Slim\Slim; $app = new Slim(); // Replace /comphppuebla/guzzle for the path to your http-verbs.php file to try this route. // curl -X GET http://localhost/comphppuebla/guzzle/contacts/1 $app->get('/contacts/:id', function ($id) { echo "Request via GET with ID = {$id}"; }); // Replace /comphppuebla/guzzle for the path to your http-verbs.php file to try this route. // curl -X POST http://localhost/comphppuebla/guzzle/contacts $app->post('/contacts', function () { echo "Request via POST"; }); // Replace /comphppuebla/guzzle for the path to your http-verbs.php file to try this route. // curl -X PUT http://localhost/comphppuebla/guzzle/contacts/2 $app->put('/contacts/:id', function ($id) { echo "Request via PUT with ID = {$id}"; }); // Replace /comphppuebla/guzzle for the path to your http-verbs.php file to try this route. // curl -X DELETE http://localhost/comphppuebla/guzzle/contacts/3 $app->delete('/contacts/:id', function ($id) { echo "Request via DELETE with ID = {$id}"; }); // Replace /comphppuebla/guzzle for the path to your http-verbs.php file to try this route. // curl -X OPTIONS http://localhost/comphppuebla/guzzle/contacts/4 $app->options('/contacts/:id', function ($id) { echo "Request via OPTIONS with ID = {$id}"; }); $app->run();