step 14: delete post view controller cakephp blog

step 14: delete post view controller cakephp blog

Open your browser to see the post list and try to delete any post "http://localhost/cakeblog/posts/admin" in cakephp blog. Opps!!! Your Browser output should look like as follows:

 

Again effect before modifying controller will result as follows:

 

To delete post working smoothly we need to make changes as follows:

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());
         }
         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'));
         }
      }
   ?>
 

Finally open your browser and try to delete any post from post "http://localhost/cakeblog/posts/delete/3" will result as follows:

 

Related Tutorial Examples

  1. step 11: admin view posts for cakephp blog
  2. step 12: add post view controller cakephp blog
  3. step 13: edit post view controller cakephp blog
  4. step 15: adding element in cakephp blog
  5. step 16: passing data into element in cakephp blog
  6. step 17: routes configuration cakephp blog project
  7. MySQL isnull() Function Example
  8. MySQL least() Function Datetime Value Example
  9. MySQL least() Function String Value Example

No comments:

Post a Comment

leave your comments here..