Create An Admin Area In CodeIgniter 3



Create An Admin Area In CodeIgniter 3

Hello friends. In this tutorial, we will learn how to create an admin area in 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 start with code

 

Step 1 – Create an admin area in CodeIgniter 3

First of all we create the directories mentioned below.

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

Now  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-&gt;load-&gt;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. Create view file named dashboard_view.php which 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>TryCatch Classes Dashboard</title>
</head>
<body style="text-align:center;padding:100px;">

<div id="container">
  <h1>TryCatch Classes Admin Dashboard!</h1>
  <h3>Admin area in CodeIgniter 3.x</h3>
  <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';

Important thing to note in Dashboard is extending Admin_Controller. In this tutorial I decided to 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. Thanks for reading this tutorial.

Don’t forget to share your doubts in the comment box and also share this post on social media and with your friends becaus“You share, I share, let’s make the world aware”.

You may want to take a look at the following related posts:

Also for more awesome tutorials, please don’t forget to like our facebook page TryCatch Classes.

Bonus: We also give training on following topics:

                       1. Angular Training in Mumbai.

2. Bootstrap Training Course in Mumbai.

3.  Web Designing Training in Mumbai.

4. UI / UX Training.

5. IOS Training Institute in Mumbai.