Last active
January 19, 2019 14:15
-
-
Save Adroit11/b89eb1efebe459a2e79f65556c655773 to your computer and use it in GitHub Desktop.
updated inde.php file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use \Psr\Http\Message\ServerRequestInterface as Request; | |
use \Psr\Http\Message\ResponseInterface as Response; | |
require '../vendor/autoload.php'; | |
$app = new \Slim\App(); | |
function availableLibraryId($id) { | |
return (int)$id && $id > 0 && $id <= 5; | |
} | |
$app->get('/', function (Request $request, Response $response) { | |
$response->getBody()->write("Welcome to the Adroit Library Demo."); | |
return $response; | |
}); | |
$app->group('/library', function () { | |
$this->map(['GET'], '', function (Request $request, Response $response) { | |
return $response->withJson(['message' => 'Welcome, please pick a libray']); | |
}); | |
$this->get('/{id}', function (Request $request, Response $response, $args) { | |
if(availableLibraryId($args['id'])) { | |
return $response->withJson(['message' => "library ".$args['id']]); | |
} | |
return $response->withJson(['message' => 'library Not Found'], 404); | |
}); | |
$this->map(['POST', 'PUT', 'PATCH'], '/{id}', function (Request $request, Response $response, $args) { | |
if(availableLibraryId($args['id'])) { | |
return $response->withJson(['message' => "library ".$args['id']." updated successfully"]); | |
} | |
return $response->withJson(['message' => 'library Not Found'], 404); | |
}); | |
$this->delete('/{id}', function (Request $request, Response $response, $args) { | |
if(availableLibraryId($args['id'])) { | |
return $response->withJson(['message' => "library ".$args['id']." deleted successfully"]); | |
} | |
return $response->withJson(['message' => 'library Not Found'], 404); | |
}); | |
}); | |
$app->run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment