step 10: making view post details for cakephp blog

step 10: making view post details for cakephp blog

Open your browser to see the available posts "http://localhost/cakeblog/posts" in cakephp blog as follows:

 

Now click anyone to see the details view "http://localhost/cakeblog/posts/view/4" in cakephp blog. Opps!!! Your Browser output should look like as follows:

 

To make the details view of a post we have to do two changes as follows:

Change #1 - Controller Modification:
Open posts Controller located at “/app/controllers/posts_controller.php" then copy & past the blow code replacing current.

/app/controllers/posts_controller.php

   <?php
      class PostsController extends AppController
      {
         var $name = 'Posts';
         function index()
         {
            $this->set('posts', $this->Post->find('all'));
         }
         function view($id = null)
         {
            $this->Post->id = $id;
            $this->set('post', $this->Post->read());
         }
      }
   ?>
 

Change #2 - view post details addition:
Make a file at “/app/views/posts/view.ctp" then copy & paste blow code into it to make the details view of the post.

/app/views/posts/view.ctp

   <!-- File: /app/views/posts/view.ctp -->
   <h1><?php echo $post['Post']['title']?></h1>
   <p><small>Created: <?php echo $post['Post']['created']?></small></p>
   <p><?php echo $post['Post']['body']?></p>
 

Finally open your browser and see the details view of any post "http://localhost/cakeblog/posts/view/4". If everything went fine you should get as follows:

 

Related Tutorial Examples

  1. step 7: controller for posts cakephp blog
  2. step 8: making model for posts cakephp blog
  3. step 9: view for posts cakephp blog cakephp examples
  4. step 11: admin view posts for cakephp blog
  5. step 12: add post view controller cakephp blog
  6. step 13: edit post view controller cakephp blog
  7. step 14: delete post view controller cakephp blog
  8. MySQL NULLIF() Nested Function Example
  9. MySQL IFNULL() Function Datetime Example
  10. MySQL coalesce() Function Example

No comments:

Post a Comment

leave your comments here..