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
- step 8: making model for posts cakephp blog
- step 9: view for posts cakephp blog cakephp examples
- step 10: making view post details for cakephp blog
- step 12: add post view controller cakephp blog
- step 13: edit post view controller cakephp blog
- step 14: delete post view controller cakephp blog
- step 15: adding element in cakephp blog
- MySQL IF() Function Number Value Example
- MySQL IF() Function Boolean Value Example
- MySQL strcmp() Function Number Value Example
No comments:
Post a Comment
leave your comments here..