// Hint [CSRF]: Protection? $app->post('/delete/:id', function ($postId) use($app, $di) { $post = new Notch\Posts($di); $postData = $post->getDetail($postId); $message = 'Post deleted successfully!'; $success = $post->delete($postId); if ($success == false) { $message = 'There was an error deleting the post!'; } $data = array('post' => $postData, 'success' => $success, 'message' => $message); $app->render('post/delete.php', $data); }); $app->get('/edit/:id', function ($postId) use($app, $di) { $post = new Notch\Posts($di); $data = array('postData' => $post->getDetail($postId)); $app->render('/post/add.php', $data); }); $app->post('/edit/:id', function ($postId) use($app, $di) { $posted = $app->request->post(); $posted['author'] = @$_SESSION['username']; $posted['id'] = $postId; $post = new Notch\Posts($di); $success = $post->save($posted); $message = 'Post saved successfully!'; if ($success == false) { $message = 'There was an error editing the post!'; } $data = array('postData' => $post->getDetail($postId), 'success' => $success, 'message' => $message); $app->render('post/add.php', $data); }); });
// Custom autoloader spl_autoload_register(function ($class) { $path = __DIR__ . '/lib/' . str_replace('\\', '/', $class) . '.php'; if (is_file($path)) { require_once $path; } }); // Build out objects $di = new Container(); $di['db'] = function () { return new Notch\Database('127.0.0.1', 'notch', 'notch42', 'notch'); }; $app = new Slim\Slim(array('debug' => true)); $app->error(function (\Exception $e) use($app) { // do nothing... }); /** * Index routing */ $app->get('/', function () use($app, $di) { // Get the most recent posts $post = new Notch\Posts($di); $postList = $post->getLatest(10); $data = array('posts' => $postList); $app->render('index/index.php', $data); }); // Other controllers require 'controller/posts.php'; require 'controller/user.php'; $app->run(); require 'templates/footer.php';