Releases: artingo/php-blog
Releases · artingo/php-blog
11. 'Edit existing post' feature
Implement the 'edit existing post' feature:
- add a new rule to the routes
- remember the current user id in a session variable
- only a post's author should have the right to edit it
- add a small
<form>with a hidden 'id' field. - create the
controllers\posts\update.phpcontroller - in
controllers\posts\create.php, create a new, emptyPostand pass it to the view - rename
views\posts\create.phptoedit.php. Insert some hidden fields. - fill all fields' values with the post's data
- modify the code in
controllers\posts\save.phpto save new and existing entries
10. 'Delete post' feature
Implement the 'delete post' feature:
- add a new rule to the routes:
'/posts/delete' => BASE_PATH . '/controllers/posts/delete.php' - 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) - upon button click, show a dialog that asks: 'Do you really want to delete this post?'.
- in the dialog, implement a small
<form method="post" action="/posts/delete">with a hidden<input name="postId" type="hidden" value="<?= $post->id ?>">. - create the
controllers\posts\delete.phpcontroller and insert this code:
$postId = $_POST['postId'];
unset($GLOBALS['posts'][$postId]);
saveData('posts');
header("location: /posts");
9. Implement the post detail view
- Create a new dynamic rule in the router.
- Implement a
getItem($view, $id)function infunctions.php. - Implement
controllers\posts\read.php. - Implement
views\posts\read.php, showing the post's details.
Step 8: Implement the 'create post' feature.
- 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
Step 6: Create a layout template
Create a layout template. To do so, follow these steps:
- Create a
views\partialssub-directory. - Cut the code of HTML
<head>, top navigation, sidebar and footer and paste it into their corresponding PhP files. requireall partials in theloadView()function.- Create a
publicfolder andcss,img,jsandwebfontssub-folders. Download the corresponding resources to these directories and modify the links to use the local copies. - Tell the PhP server to use the
publicdirectory with these parameters:
php -S localhost:8080 -t public
Step 5: insert HTML code into 'posts.php'
Chose a UI framework. We recommend:
1. Bootstrap or Material Design Lite
2. Search for a demo layout page and copy its HTML code to the views\posts.php file.
For example: AdminLTE starter page
3. Open the controllers\posts.php page in your browser and check the result.
4. Correct all the CSS, JS and image references using CDN links (for starters).
5. Remove all unnecessary UI elements and place your own labels.
Step 4: create views
- Create PHP/HTML views in the
viewsfolder. Write aviewfunction that loads data from the corresponding JSON file and loads the proper view template to display it.
Step 3: create controllers
- Create controllers in the
controllersfolder that load and display the JSON data.
Step 2: create JSON files
- Write scripts that generate test data in JSON format.
Step 1 - create model
Follow these steps to continuously build a server-side Blog web app with PhP.
- Create the model classes in the
modelfolder.