This section will show you how to create the admin page for our blog, with pages to create and edit blog posts. By the end of this part you should have a functioning admin page (that requires login!) displaying a list of posts from the database, with links to view, edit, or delete the post.
Create edit.php at this location :
/application/views/admin/edit.php
Copy the code below :
Controller
Our final controller will tie all of our admin page logic together, calling the method functions and loading the view files we just created as requested.
Create admin.php at this location :
/application/controllers/admin.php
Copy the code below :
Change Tank Auth’s Behavior
Since we’re only using the Tank Auth authentication for our admin page, instead of our entire application, we need to tweak it’s functionality a bit.
Currently logging in to the admin section will take us directly back to the main blog index (although the user is successfully logged in), and then clicking the ‘Admin’ link one more time is required to gain access to the Admin index page. We’re going to fix this by redirecting back to our ‘Admin’ controller index immediately after a successful login.
Open auth.php located here:
/application/controllers/auth.php
Find the line that says (around line 71) :
Replace with the code below and save :
Go back to auth.php, find method ‘logout’ (around line 106) :
Replace with the code below and save :
Before our blog is complete we’re going to want to change to tell the Auth controller to wrap it’s loaded views around our HTML wrappers just like our other controllers.
Find these lines one at a time:
$this->load->view(‘auth/login_form’, $data);
$this->load->view(‘auth/register_form’, $data);
$this->load->view(‘auth/forgot_password_form’, $data);
$this->load->view(‘auth/reset_password_form’, $data);
$this->load->view(‘auth/change_password_form’, $data);
$this->load->view(‘auth/unregister_form’, $data);
For EACH line you must insert the following lines :
Insert this BEFORE the line:
$this->load->view(‘template/html_head’, $data);
Insert this AFTER the line:
$this->load->view(‘template/html_tail’, $data);
Using the same HTML wrappers over and over, we can quickly create many lovely views for the Tank Auth forms easily.
Conclusion
You should now have a simple blog that displays posts as well as having a separate admin section to serve as a very simple “content management system” CMS.
hope this helped you!
I hope all goes well! Look forward to seeing more post from Anjaneya Vadivel in the future.
Please comment below!
We’re gonna want to add the methods to insert, update, and delete posts. With CodeIgniter manipulating database data is simplified.
Go back to the Posts model located at :
/application/models/posts.php
Copy the extra methods to our Post model from the code below :
- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- class Posts extends CI_Model
- {
- function __construct()
- {
- parent::__construct();
- }
- // If $postId is NULL, gets all posts, otherwise get single post from db
- // returns $post[]
- public function get_posts($postId)
- {
- $post=array();
- if ($postId !== null){
- $query = $this->db->get_where('posts', array('id' =>$postId));
- if ($query->num_rows() == 1) {
- foreach($query->result() as $row){
- $post['id'] = $row->id;
- $post['title'] = $row->title;
- $post['summary'] = $row->summary;
- $post['content'] = $row->content;
- }
- return $post;
- }
- } else {
- $query = $this->db->get('posts');
- if ($query->num_rows() !== 0 ){
- foreach($query->result() as $row){
- $post['id'][] = $row->id;
- $post['title'][] = $row->title;
- $post['summary'][] = $row->summary;
- $post['content'][] = $row->content;
- }
- return $post;
- }
- }
- }
- function insert_post($data){
- $this->db->insert('posts', $data);
- return;
- }
- function update_post($postId, $data){
- $id = $postId;
- $this->db->where('id',$id);
- $this->db->update('posts', $data);
- return;
- }
- function delete_post($postId){
- $id = $postId;
- $this->db->delete('posts',array('id' => $id));
- return;
- }
- }
Views
Once again we’re going to need to create a few simple view files for our admin index page, a new html head wrapper file that will have a ‘Logout’ link instead of an ‘Admin’ link, and forms for creating/editing posts.
Create admin_html_head.php at this location :
/application/views/template/admin_html_head.php
Copy the code below :
- <!DOCTYPE html>
- <head>
- <link rel="stylesheet" type="text/css" href="<?php echobase_url('style.css');?>">
- </head>
- <body>
- <h1><?php echo anchor('','My Blog'); ?></h1>
- <div id="loginDiv"><?php echo anchor('admin/logout','Logout');?></div>
- <hr/>
Create index.php at this location :
/application/views/admin/index.php
Copy the code below :
- <?php
- echo '<p>Welcome To The Admin Page '.$username.'! All posts available for edit or deletion is listed below.</p><br/>';
- echo anchor('admin/create','Create New Post');
- $count = count($post['id']);
- for ($i=0;$i<$count;$i++)
- {
- echo '<div class="postDiv">';
- echo '<h4>'.$post['title'][$i];
- echo anchor('blog/view/'.$post['id'][$i],' [view]');
- echo anchor('admin/edit/'.$post['id'][$i],' [edit]');
- echo anchor('admin/delete/'.$post['id'][$i],' [delete]</h4>');
- echo '<p>'.$post['summary'][$i].'</p>';
- echo '</div>';
- }
- ?>
reate create.php at this location :
/application/views/admin/create.php
Copy the code below :
- <?php
- echo validation_errors();
- ?>
- <h4>Create A New Post Below</h4>
- <form action="" method="post" >
- <p>Title:</p>
- <input type="text" name="title" size="50"/><br/>
- <p>Summary:</p>
- <textarea name="summary" rows="2" cols="50"></textarea><br/>
- <p>Post Content:</p>
- <textarea name="content" rows="6" cols="50"></textarea><br/>
- <input type="submit" value="Save" />
- <?php
- echo anchor('admin','Cancel');
- ?>
- </form>
Create edit.php at this location :
/application/views/admin/edit.php
Copy the code below :
- <?php
- echo validation_errors();
- ?>
- <h4>Edit "<?php echo $post['title']; ?>" Below</h4>
- <form action="" method="post" >
- <p>Title:</p>
- <input type="text" name="title" size="50" value="<?php echo$post['title']; ?>"/><br/>
- <p>Summary:</p>
- <textarea name="summary" rows="2" cols="50"><?php echo$post['summary']; ?>
- </textarea><br/>
- <p>Post Content:</p>
- <textarea name="content" rows="6" cols="50"><?php echo$post['content']; ?>
- </textarea><br/>
- <input type="submit" value="Save" />
- <?php
- echo anchor('admin','Cancel');
- ?>
- </form>
Controller
Our final controller will tie all of our admin page logic together, calling the method functions and loading the view files we just created as requested.
Create admin.php at this location :
/application/controllers/admin.php
Copy the code below :
- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- class Admin extends CI_Controller
- {
- function __construct()
- {
- parent::__construct();
- $this->load->helper(array('form','url'));
- $this->load->library(array('tank_auth','form_validation'));
- $this->load->model('posts');
- }
- function index() {
- if (!$this->tank_auth->is_logged_in()) {
- redirect('/auth/login');
- } else {
- $data['post'] = $this->posts->get_posts(null);
- $data['userId'] = $this->tank_auth->get_user_id();
- $data['username'] = $this->tank_auth->get_username();
- $this->load->view('template/admin_html_head', $data);
- $this->load->view('admin/index', $data);
- $this->load->view('template/html_tail', $data);
- }
- }
- function create(){
- $data['userId'] = $this->tank_auth->get_user_id();
- $data['username'] = $this->tank_auth->get_username();
- $this->form_validation->set_rules('title','title','required');
- $this->form_validation->set_rules('summary','summary','required');
- $this->form_validation->set_rules('content','content','required');
- if($this->form_validation->run()==FALSE)
- {
- $this->load->view('template/admin_html_head',$data);
- $this->load->view('admin/create',$data);
- $this->load->view('template/html_tail',$data);
- } else {
- $data = $_POST;
- $this->posts->insert_post($data);
- redirect('admin');
- }
- }
- function edit($postId){
- $data['userId'] = $this->tank_auth->get_user_id();
- $data['username'] = $this->tank_auth->get_username();
- $data['post'] = $this->posts->get_posts($postId);
- $this->form_validation->set_rules('title','title','required');
- $this->form_validation->set_rules('summary','summary','required');
- $this->form_validation->set_rules('content','content','required');
- if($this->form_validation->run()==FALSE)
- {
- $this->load->view('template/admin_html_head',$data);
- $this->load->view('admin/edit',$data);
- $this->load->view('template/html_tail',$data);
- } else {
- $data = $_POST;
- $this->posts->update_post($postId, $data);
- redirect('admin');
- }
- }
- function delete($postId){
- $this->posts->delete_post($postId);
- redirect('admin');
- }
- function logout(){
- redirect ('/auth/logout');
- }
- }
Change Tank Auth’s Behavior
Since we’re only using the Tank Auth authentication for our admin page, instead of our entire application, we need to tweak it’s functionality a bit.
Currently logging in to the admin section will take us directly back to the main blog index (although the user is successfully logged in), and then clicking the ‘Admin’ link one more time is required to gain access to the Admin index page. We’re going to fix this by redirecting back to our ‘Admin’ controller index immediately after a successful login.
Open auth.php located here:
/application/controllers/auth.php
Find the line that says (around line 71) :
- // success
- redirect('');
Replace with the code below and save :
- // success
- redirect('admin');
Go back to auth.php, find method ‘logout’ (around line 106) :
- function logout()
- {
- $this->tank_auth->logout();
- $this->_show_message($this->lang->line('auth_message_logged_out'));
- }
Replace with the code below and save :
- function logout()
- {
- $this->tank_auth->logout();
- redirect('');
- //$this->_show_message($this->lang->line('auth_message_logged_out'));
- }
Before our blog is complete we’re going to want to change to tell the Auth controller to wrap it’s loaded views around our HTML wrappers just like our other controllers.
Find these lines one at a time:
$this->load->view(‘auth/login_form’, $data);
$this->load->view(‘auth/register_form’, $data);
$this->load->view(‘auth/forgot_password_form’, $data);
$this->load->view(‘auth/reset_password_form’, $data);
$this->load->view(‘auth/change_password_form’, $data);
$this->load->view(‘auth/unregister_form’, $data);
For EACH line you must insert the following lines :
Insert this BEFORE the line:
$this->load->view(‘template/html_head’, $data);
Insert this AFTER the line:
$this->load->view(‘template/html_tail’, $data);
Using the same HTML wrappers over and over, we can quickly create many lovely views for the Tank Auth forms easily.
Conclusion
You should now have a simple blog that displays posts as well as having a separate admin section to serve as a very simple “content management system” CMS.
hope this helped you!
I hope all goes well! Look forward to seeing more post from Anjaneya Vadivel in the future.
Please comment below!
Nice Tutorial...i got useful information from this tutorial,here is a way to findsimple registration form using codeigniter
ReplyDelete