$app->response()->header('Content-Type', 'application/json'); $user = $db->users()->where('id', $id); if ($data = $user->fetch()) { echo json_encode(array("id" => $data["id"], "fname" => $data["fname"], "lname" => $data["lname"], "title" => $data["title"], "username" => $data["username"], "role" => $data["role"], "email" => $data["email"], "status" => $data["status"])); } else { echo json_encode(array("status" => false, "message" => "User ID {$id} does not exist")); } } catch (Exception $e) { echo $e->getMessage(); } }); /* User Insert */ $app->post('/user', function () use($app, $db) { try { $app->response()->header('Content-Type', 'application/json'); $user = $app->request()->post(); $result = $db->users->insert($user); updatePassword($result["id"], $result["password"]); echo json_encode(array("id" => $result["id"], "message" => "Add user successfully")); } catch (Exception $e) { echo $e->getMessage(); } }); /* User Update */ $app->put('/user/:id/:jsondata', function ($id, $jsondata) use($app, $db) { try { $updateUserData = json_decode($jsondata, true); $app->response()->header('Content-Type', 'application/json'); $user = $db->users()->where('id', $id); if ($user) { $result = $user->update($updateUserData);
$app->view->parserOptions = array('charset' => 'utf-8', 'cache' => realpath('templates/cache'), 'auto_reload' => true, 'strict_variables' => false, 'autoescape' => true, 'debug' => true); $app->view->parserExtensions = array(new \Slim\Views\TwigExtension(), new \Twig_Extension_Debug()); $app->get('/', function () use($app) { $data = array("intro" => "<div>\n <div class='jumbotron'>\n <div class='page-header'>\n <h1>Task App <small>Welcome to Task App</small></h1>\n </div>\n <p>This is a tiny user-task manager Application</p>\n </div>\n </div>"); $app->render('frontpage.twig', $data); }); $app->get('/tasks', function () use($app) { $app->render('tasks.twig'); }); $app->get('/users', function () use($app) { $app->render('users.twig'); }); // Login route MUST be named 'login' $app->map('/login', function () use($app) { $username = null; if ($app->request()->isPost()) { $username = $app->request->post('username'); $password = $app->request->post('password'); $result = $app->authenticator->authenticate($username, $password); if ($result->isValid()) { $app->redirect('.'); } else { $messages = $result->getMessages(); $app->flashNow('error', $messages[0]); } } $app->render('login.twig', array('username' => $username)); })->via('GET', 'POST')->name('login'); $app->get('/logout', function () use($app) { if ($app->auth->hasIdentity()) { $app->auth->clearIdentity();