Created
May 17, 2015 15:16
-
-
Save geggleto/02c57b049a16aaee9d3a to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Space; | |
| class RouteManager { | |
| public function __construct() { | |
| $app = \Slim\Slim::getInstance(); | |
| $app->get('/', function () { | |
| print "hi"; | |
| }); | |
| //user stuff | |
| $app->get('/user', \Space\Controllers\UserController::listMethods()); | |
| $app->get('/user/:id', \Space\Controllers\UserController::getUser()); | |
| $app->post('/user', \Space\Controllers\UserController::createUser()); | |
| $app->put('/user/:id', \Space\Controllers\UserController::updateUser()); | |
| $app->delete('/user/:id', \Space\Controllers\UserController::deleteUser()); | |
| } | |
| } |
This file contains hidden or 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 Space\Controllers; | |
| use \Space\Models\UserModel as User; | |
| class UserController { | |
| public static function getUser() { | |
| return function ($id) { | |
| $app = \Slim\Slim::getInstance(); | |
| $user = User::find($id); | |
| if (!is_null($user) && $user instanceof User) { | |
| $app->response->setStatus(200); | |
| print $user->toJson(); | |
| } else { | |
| $app->response->setStatus(404); | |
| $app->response->setBody("Not Found"); | |
| } | |
| }; | |
| } | |
| public static function createUser() { | |
| return function () { | |
| $app = \Slim\Slim::getInstance(); | |
| $attr = $app->request->post(); | |
| try { | |
| $user = new User($attr); | |
| $user->save(); | |
| $app->response->setStatus(201); | |
| print $user->toJson(); | |
| } catch (Exception $e) { | |
| $app->response->setStatus(404); | |
| } | |
| }; | |
| } | |
| public static function updateUser() { | |
| return function ($id) { | |
| $app = \Slim\Slim::getInstance(); | |
| $attr = $app->request->post(); | |
| try { | |
| $user = User::find($id); | |
| if ($user instanceof User) { | |
| $user->update($attr); | |
| $user->save(); | |
| $app->response->setStatus(204); | |
| } else { | |
| $app->response->setStatus(404); | |
| } | |
| } catch (Exception $e) { | |
| $app->response->setStatus(400); | |
| } | |
| }; | |
| } | |
| public static function deleteUser() { | |
| return function ($id) { | |
| //Not supported | |
| $app->response->setStatus(403); | |
| }; | |
| } | |
| public static function listMethods() { | |
| return function ($id) { | |
| }; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment