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.

INI

1

2

3

4

5

-application/

--controllers/

---admin/

--views/

---admin/

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

application/controllers/admin/Dashboard.php

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?phpdefined('BASEPATH')ORexit('No direct script access allowed');

classDashboardextendsAdmin_Controller

{

function__construct()

{

parent::__construct();

}

publicfunctionindex()

{

$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:

application/views/admin/dashboard_view.php

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<?phpdefined('BASEPATH')ORexit('No direct script access allowed');?>

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>Welcome tothe Dashboard</title>

</head>

<body>

<div id="container">

<h1>Welcome tothe Dashboard!</h1>

<pclass="footer">Pagerenderedin<strong>{elapsed_time}</strong>seconds.<?phpecho(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:

application/config/development/routes.php

PHP

1

2

3

4

5

6

7

8

9

<?php

defined('BASEPATH')ORexit('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:

it is because dashboard_view.php should be put on folder application/views/admin/dashboard_view.php instead of application/views/dashboard_view.php
(previously i experienced the same problem Sojib because of following the view path from your tutorial, i think you need to edit it Sir Avenirer)

Hello,
first: excuse my english, i ‘m french 😉
There is something wrong, or i don’t understand:
You write: “Now we will create a controller named Dashboard (Dashboard.php)inside the application/controllers/admin directory:”
and i see in the script’s title: “application/controllers/Dashboard.php”…
What is the right way, please :
/controllers/Dashboard.php or /controllers/admin/Dashboard.php ?
Thanks

Hello, I have just installed codeigniter-3.0.3 but there is not “application/config/developement/” folder. There is only “application/config/”. There I have modified routes.php as your example. But i get error “404 file not found”.

Files are in the right folders:
– controllers/admin/Dashboard.php
– views/admin/dashboard_view.php

The only difference is that my base url is “http://localhost/codeigniter-3.0.3” (I have update config.php $config[‘base_url’] = ‘http://localhost/codeigniter-3.0.3/’;)

Remove “-3.0.3” from the folder name… and from the config. Also, did you follow the tutorials from the previous series (http://avenir.ro/codeigniter-tutorials/)? Is not really required, but you need to have a viable installation of your CI framework. At least go from episode 1 to episode 7 🙂

Woooow… Who told you that??? Don’t do that, pleaaaaaaase. Where did you get that idea from? Did you at least read the tutorials that were prerequisite for this series? Or you just want to seem professional?