step 13: edit post view controller cakephp blog

step 13: making edit post view controller cakephp blog

Open your browser to edit any post before modifying controller "http://localhost/cakeblog/posts/edit" in cakephp blog. Opps!!! Your Browser output should look like as follows:

 

To edit post working smoothly we need to make 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());
         }
         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'));
               }
            }
         }
      }
   ?>

Agin open your browser to see the effect before making view to edit post "http://localhost/cakeblog/posts/edit" in cakephp blog. Opps!!! Your Browser output should look like as follows:

 

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

/app/views/posts/edit.ctp

   <!-- File: /app/views/posts/edit.ctp -->

   <h1>Edit Post</h1>
   <?php
      echo $form->create('Post', array('action' => 'edit'));
      echo $form->input('title');
      echo $form->input('body', array('rows' => '3'));
      echo $form->input('id', array('type'=>'hidden'));
      echo $form->end('Save Post');
   ?>
 

Now open your browser and see the edit form to edit post "http://localhost/cakeblog/posts/edit/3".

 

Finally editted data will guide you to as follows:

 

Related Tutorial Examples

  1. step 10: making view post details for cakephp blog
  2. step 11: admin view posts for cakephp blog
  3. step 12: add post view controller cakephp blog
  4. step 14: delete post view controller cakephp blog
  5. step 15: adding element in cakephp blog
  6. step 16: passing data into element in cakephp blog
  7. step 17: routes configuration cakephp blog project
  8. MySQL strcmp() Function Example :: on Equals,Large.. Values
  9. MySQL interval() Function Example
  10. MySQL coalesce() Function Example

No comments:

Post a Comment

leave your comments here..