step 16: passing data into element in cakephp blog

step 16: passing data variable into element in cakephp blog

Passing data variable from controller to an element in cakephp blog we have to make two changes as follows:

1. Controller Modification:
Open posts Controller located at “/app/controllers/posts_controller.php" then copy & past the blow code replacing the 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());
         }
         function admin()
         {
            $this->set('posts', $this->Post->find('all'));
         }
         function add()
         {
            if (!empty($this->data))
            {
               if ($this->Post->save($this->data))
               {
                  $this->Session->setFlash('Your post has been saved.');
                  $this->redirect(array('action' => 'admin'));
               }
            }
         }
         function edit($id = null)
         {
            $this->Post->id = $id;
            if (empty($this->data))
            {
               $this->data = $this->Post->read();
            }
            else
            {
               if ($this->Post->save($this->data))
               {
                  $this->Session->setFlash('Your post has been updated.');
                  $this->redirect(array('action' => 'admin'));
               }
            }
         }
         function delete($id)
         {
            $this->Post->delete($id);
            $this->Session->setFlash('The post with id: '.$id.' has been deleted.');
            $this->redirect(array('action'=>'admin'));
         }
         function latest()
         {
            $posts = $this->paginate();
            if (isset($this->params['requested']))
            {
               return $posts;
            }
            else
            {
               $this->set('posts', $posts);
            }
         }
      }
   ?>

"latest()" is the method which is responsible to push data to "latest" element

 

2. Element Modification:
Now open up the latest Element file located at “/app/views/elements/latest.ctp" then replace the code as bellow.

/app/views/elements/latest.ctp

   <h2>Recent Posts</h2>

   <?php $posts = $this->requestAction('posts/latest/sort:created/direction:desc/limit:3'); ?>

   <ul>
      <?php foreach ($posts as $post): ?>
         <li><?php echo $html->link($post['Post']['title'], array('action' => 'view',$post['Post']['id']));?></li>
      <?php endforeach; ?>
   </ul>
 

Finally open your browser and get the element holding data from controller in CakePHP Blog Project "http://localhost/cakeblog/" as follows:

 

Related Tutorial Examples

  1. step 13: edit post view controller cakephp blog
  2. step 14: delete post view controller cakephp blog
  3. step 15: adding element in cakephp blog
  4. step 17: routes configuration cakephp blog project
  5. MySQL greatest() Function Datetime Example
  6. MySQL now() Datetime Function Example
  7. MySQL greatest() Function String Value Example
  8. MySQL Stored Procedure Function: Making hello world Function using aliase Example
  9. MySQL Stored Procedure: Creating Table using Stored Procedure Example

No comments:

Post a Comment

leave your comments here..