step 11: admin view posts for cakephp blog

step 11: making admin view posts for cakephp blog

click admin link to see admin view of posts "http://localhost/cakeblog/posts/admin" in cakephp blog. Opps!!! Your Browser output should look like as follows:

 

To make admin view posts we have 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'));
               }
            }
         }
      }
   ?>

again click admin link to see admin view of posts "http://localhost/cakeblog/posts/admin" in cakephp blog. Opps!!! Your Browser output should look like as follows as view is not created yet:

 
 

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

/app/views/posts/admin.ctp

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

   <h1>Blog Posts Admin</h1>
   <p><?php echo $html->link("Add Post", array('action'=>'add')); ?>
   <table>
   <tr>
   <th>Id</th>
   <th>Title</th>
   <th>Created</th>
   <th>Action</th>
   </tr>
   <?php foreach ($posts as $post): ?>
   <tr>
   <td><?php echo $post['Post']['id']; ?></td>
   <td>
   <?php echo $html->link($post['Post']['title'],array('action'=>'view', 'id'=>$post['Post']['id']));?>
   </td>
   <td><?php echo $post['Post']['created']; ?></td>
   <td>
      <?php echo $html->link('Edit', array('action'=>'edit', 'id'=>$post['Post']['id']));?>
         |
      <?php echo $html->link('Delete',array('action'=>'delete', 'id'=>$post['Post']['id']),null,'Are you sure?')?>
   </td>
   </tr>
   <?php endforeach; ?>

   </table>
 

Finally open your browser and see admin view posts "http://localhost/cakeblog/posts/admin". If everything went fine you should get as follows:

 

Related Tutorial Examples

  1. step 8: making model for posts cakephp blog
  2. step 9: view for posts cakephp blog cakephp examples
  3. step 10: making view post details for cakephp blog
  4. step 12: add post view controller cakephp blog
  5. step 13: edit post view controller cakephp blog
  6. step 14: delete post view controller cakephp blog
  7. step 15: adding element in cakephp blog
  8. MySQL IF() Function Number Value Example
  9. MySQL IF() Function Boolean Value Example
  10. MySQL strcmp() Function Number Value Example

No comments:

Post a Comment

leave your comments here..