1. Admin dashboard for editing posts
- in the "render()" method of the class Blog, if the admin is logged, Edit button is displayed otherwise normal listing of posts
- new path /edit/{slug} added to route.xml as a result of the above
2. Edit posts and save them to database
- the edit post page should look exactly like the new post entry just that it should be populated with the values from DB. So I added the file "edit_post_entry.php" under templates.
Inside I added value=<?php echo '"'.$author.'"'; ?> (same for the title and category).
- issue: I do not want to take the file edit_post_entry.php and include it directly into the main template, I want first to process it (interpret ) with the local variables replacing the actual "value=" in the HTML form.
The post "Read echo'ed output from another PHP file" http://stackoverflow.com/questions/631388/read-echoed-output-from-another-php-file showed me the way:
ob_start(); // begin collecting output
include 'myfile.php';
$result = ob_get_clean(); // retrieve output from myfile.php, stop buffering
//$result will then contain the text.
- as you could see the slug is used to identify the post.Only for update I am using the post Id as unique identifier and the new slug is saved in database if the title of the blog is changed.
I will put in the form (edit_post_entry.php) a hidden field to store the post Id taken from database which will be used in the SQL update statement:
$new_slug=SlugGenerator::slugify($Title);
$stmt = $this->db->prepare("UPDATE blogposts SET Category=:field1, Author=:field2, ActualPost=:field3,title=:field4,slug=:field5 WHERE Id=:field_id;");
$stmt->bindParam(':field_id',$PostID, PDO::PARAM_INT);
$stmt->bindParam(':field1', $Category);
$stmt->bindParam(':field2', $Author);
$stmt->bindParam(':field3', $Text);
$stmt->bindParam(':field4', $Title);
$stmt->bindParam(':field5', $new_slug);
$stmt->execute();
3. Delete posts
First I get the post using "getPost($post_slug)" and from the output the post Id is read and the delete is applied to that Id.
The code is available in my first release on GitHub, v0.4: https://github.com/cristianpana86/myFrontController/releases/tag/v0.4