Skip to content

Releases: artingo/php-blog

11. 'Edit existing post' feature

27 Nov 21:09
Compare
Choose a tag to compare

Implement the 'edit existing post' feature:

  1. add a new rule to the routes
  2. remember the current user id in a session variable
  3. only a post's author should have the right to edit it
  4. add a small <form> with a hidden 'id' field.
  5. create the controllers\posts\update.php controller
  6. in controllers\posts\create.php, create a new, empty Post and pass it to the view
  7. rename views\posts\create.php to edit.php. Insert some hidden fields.
  8. fill all fields' values with the post's data
  9. modify the code in controllers\posts\save.php to save new and existing entries

10. 'Delete post' feature

23 Nov 12:02
Compare
Choose a tag to compare

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");

9. Implement the post detail view

22 Nov 15:57
Compare
Choose a tag to compare
  1. Create a new dynamic rule in the router.
  2. Implement a getItem($view, $id) function in functions.php.
  3. Implement controllers\posts\read.php.
  4. Implement views\posts\read.php, showing the post's details.

Step 8: Implement the 'create post' feature.

17 Nov 10:37
Compare
Choose a tag to compare
  1. Implement the create post feature. For this:
    1. Create the controllers\posts directory and move controllers\posts.php to constrollers\posts\index.php. Correct all necessary links.
    2. Create the views\posts directory and move views\posts.php to views\posts\index.php. Correct all necessary links.
    3. Create both controllers\posts\create.php and views\posts\create.php.
    4. Add '/posts/create' => BASE_PATH . '/controllers/posts/create.php' to the $routes in public\index.php.
    5. In views\posts\create.php, insert a HTML form with these fields:
      1. title as simple input field,
      2. categories[] as multiple select box,
      3. and body as textarea. You may use a rich text editor like TinyMCE.
      4. You may add jQuery Validation to it.
      5. Add method="post" action="/posts/save" to the <form> tag.
      6. Add '/posts/save' => BASE_PATH . '/controllers/posts/store.php' to the $routes.
    6. In functions.php, write a saveData($key, $newEntry) function:
      1. load the corresponding JSON file with loadData(key)
      2. add the $newEntry to $data[]
      3. save the updated JSON file.
    7. Implement controllers\posts\store.php:
      1. create a new Post with the fields submitted in the form:

        $newPost = new Post($_POST['title'], $_POST['body'], 1, $_POST['categories']);
      2. save that post: saveData('posts', $newPost);
      3. redirect to the posts overview page: header("location: /posts");

Step 6: Create a layout template

13 Nov 22:53
Compare
Choose a tag to compare

Create a layout template. To do so, follow these steps:

  1. Create a views\partials sub-directory.
  2. Cut the code of HTML <head>, top navigation, sidebar and footer and paste it into their corresponding PhP files.
  3. require all partials in the loadView() function.
  4. Create a public folder and css, img, js and webfonts sub-folders. Download the corresponding resources to these directories and modify the links to use the local copies.
  5. Tell the PhP server to use the public directory with these parameters:

    php -S localhost:8080 -t public

Step 5: insert HTML code into 'posts.php'

13 Nov 15:32
Compare
Choose a tag to compare

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

12 Nov 22:07
Compare
Choose a tag to compare
  1. Create PHP/HTML views in the views folder. Write a view function that loads data from the corresponding JSON file and loads the proper view template to display it.

Step 3: create controllers

12 Nov 21:40
Compare
Choose a tag to compare
  1. Create controllers in the controllers folder that load and display the JSON data.

Step 2: create JSON files

12 Nov 21:32
Compare
Choose a tag to compare
  1. Write scripts that generate test data in JSON format.

Step 1 - create model

12 Nov 21:27
Compare
Choose a tag to compare

Follow these steps to continuously build a server-side Blog web app with PhP.

  1. Create the model classes in the model folder.