Step 1 – Create an admin area in CodeIgniter 3

(You may be under the wrong impression that this is the first lesson in creating a CMS using CodeIgniter 3. Actually, this continues a previous series: http://avenir.ro/codeigniter-tutorials/. You can find out more about the prerequisites here: http://avenir.ro/create-cms-using-codeigniter-3/)

Every respectable blog must have an admin area. In CodeIgniter there are a lot of methods to create such an area. In this tutorial we will simply make a directory named admin inside the controllers directory and also an admin directory inside the views directory.

So let’s get to work

First of all we create create the directories mentioned before.

-application/
- -controllers/
- - -admin/
- -views/
- - -admin/

Now we will create a controller named Dashboard (Dashboard.php)inside the application/controllers/admin directory:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Dashboard extends Admin_Controller
{

  function __construct()
  {
    parent::__construct();
  }

  public function index()
  {
    $this->load->view('admin/dashboard_view');
  }
}

As you can see, the index() method of the dashboard class will load dashboard_view which is loaded from the views/admin directory. For starters the dashboard_view.php will look like this:

<?php defined('BASEPATH') OR exit('No direct script access allowed');?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to the Dashboard</title>
</head>
<body>

<div id="container">
  <h1>Welcome to the Dashboard!</h1>
  <p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds. <?php echo (ENVIRONMENT === 'development') ? 'CodeIgniter Version <strong>' . CI_VERSION . '</strong>' : '' ?></p>
</div>

</body>
</html>

After we’ve done all this, we must make sure that when someone goes to http://localhost/admin, he/she will be taken to the dashboard. To do this we simply add a route inside the routes.php file. Now, the file will look like this:

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = TRUE;

$route['admin'] = 'admin/dashboard';

Also, one thing to note is that the Dashboard is extending Admin_Controller. In this tutorial I decided to follow Jim Parry’s example in creating more than one single MY_Controller. So, MY_Controller.php will look like this for now:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{

  function __construct()
  {
    parent::__construct();
  }
}

class Admin_Controller extends MY_Controller
{
  function __construct()
  {
    parent::__construct();
  }
}

class Public_Controller extends MY_Controller
{
  function __construct()
  {
    parent::__construct();
  }
}

That’s it. We now have an admin area. In the next tutorial we will create the basic template for the dashboard’s pages and after that we will secure the dashboard.

0 thoughts on “Step 1 – Create an admin area in CodeIgniter 3

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>