Step 8: Implement the 'create post' feature.
·
10 commits
to master
since this release
- Implement the
create postfeature. For this:- Create the
controllers\postsdirectory and movecontrollers\posts.phptoconstrollers\posts\index.php. Correct all necessary links. - Create the
views\postsdirectory and moveviews\posts.phptoviews\posts\index.php. Correct all necessary links. - Create both
controllers\posts\create.phpandviews\posts\create.php. - Add
'/posts/create' => BASE_PATH . '/controllers/posts/create.php'to the$routesinpublic\index.php. - In
views\posts\create.php, insert a HTML form with these fields:titleas simple input field,categories[]as multiple select box,- and
bodyas textarea. You may use a rich text editor likeTinyMCE. - You may add jQuery Validation to it.
- Add
method="post" action="/posts/save"to the<form>tag. - Add
'/posts/save' => BASE_PATH . '/controllers/posts/store.php'to the$routes.
- In
functions.php, write asaveData($key, $newEntry)function:- load the corresponding JSON file with
loadData(key) - add the
$newEntryto$data[] - save the updated JSON file.
- load the corresponding JSON file with
- Implement
controllers\posts\store.php:- create a new Post with the fields submitted in the form:
$newPost = new Post($_POST['title'], $_POST['body'], 1, $_POST['categories']); - save that post:
saveData('posts', $newPost); - redirect to the posts overview page:
header("location: /posts");
- create a new Post with the fields submitted in the form:
- Create the