Skip to content

10. 'Delete post' feature

Choose a tag to compare

@artingo artingo released this 23 Nov 12:02
· 7 commits to master since this release

Implement the 'delete post' feature:

  1. add a new rule to the routes:

    '/posts/delete' => BASE_PATH . '/controllers/posts/delete.php'
  2. in views\posts\read.php, add a 'delete' button that is only visible if the current user is identical to the post's user:

    if($_SESSION['currentUser'] == $post->userId)
  3. upon button click, show a dialog that asks: 'Do you really want to delete this post?'.
  4. in the dialog, implement a small <form method="post" action="/posts/delete"> with a hidden <input name="postId" type="hidden" value="<?= $post->id ?>">.
  5. create the controllers\posts\delete.php controller and insert this code:
$postId = $_POST['postId'];
unset($GLOBALS['posts'][$postId]);
saveData('posts');
header("location: /posts");