Last active
January 19, 2019 20:20
-
-
Save Adroit11/f69e306d9b3a3a0d35ccc9568308ecc3 to your computer and use it in GitHub Desktop.
libraryRoute.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 | |
namespace Library\App; | |
use \Psr\Http\Message\ServerRequestInterface as Request; | |
use \Psr\Http\Message\ResponseInterface as Response; | |
class LibraryRoute | |
{ | |
/** | |
* Stores an instance of the Slim application. | |
* | |
* @var \Slim\App | |
*/ | |
private $app; | |
public function __construct() { | |
$app = new \Slim\App(); | |
$app->get('/', function (Request $request, Response $response) { | |
$response->getBody()->write("Welcome to the Adroit Library Demo."); | |
return $response; | |
}); | |
$app->group('/library', function () { | |
$availableLibraryId = function ($id) { | |
return (int)$id && $id > 0 && $id <= 5; | |
}; | |
$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) use ($availableLibraryId) { | |
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) use ($availableLibraryId) { | |
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) use ($availableLibraryId) { | |
if($availableLibraryId($args['id'])) { | |
return $response->withJson(['message' => "library ".$args['id']." deleted successfully"]); | |
} | |
return $response->withJson(['message' => 'library Not Found'], 404); | |
}); | |
}); | |
$this->app = $app; | |
} | |
/** | |
* Get an instance of the application. | |
* | |
* @return \Slim\App | |
*/ | |
public function get() | |
{ | |
return $this->app; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment