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.

34 comments

      1. 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)

  1. 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

    1. Tu peux parler aussi in francais… Pardonne-moi. Tu dois creer le fichier dans “application/controllers/admin”.

      1. thanks.
        It’s the same for the next pages, we should understand :
        not: application/controllers/Dashboard.php
        but—> application/controllers/admin/Dashboard.php

  2. Bonjour,
    j’ai suivi le tuto, c’est bien présenté. Merci.
    Mon souci c’est sur le “routes”, doit-on créer un répertoire development et un nouveau fichier routes.php dedans (application/config/developement/routes.php)? Pourquoi on n’utilise pas le routes.php dans le config.
    Je ne sais pas pourquoi aussi, car j’ai la page 404 quand je fais http://localhost/admin
    Bonne journé

    1. Tu n’est pas oblige a faire le “routes.php” dans le config/developement. Tu peux aussi le fair dat le config. Hmmm… it’s easier in english… Can you send me your application directory in a zip file at avenir.ro@gmail.com? Thank you.

  3. 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/’;)

    What’s wrong?

    thank you

  4. For codeigniter-3.0.3 , you need to change one piece code of line in the file

    application/controllers/admin/Dashboard.php

    class dashboard extends Admin_Controller

    With

    class dashboard extends CI_Controller

    1. 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?

  5. Got below Error Msg

    Message: touch(): Unable to create file sessions/ci_session4dc0e26a2bb79fc8a06893ce8fbd19b9e1974a0b because No such file or directory
    Filename: drivers/Session_files_driver.php

    Did I sth wrong?

  6. Hi,

    Thanks for your tutorial. I’ve followed your steps exactly, but I am stuck at displaying my admin page. When I visit http://localhost/admin, it displays the following:

    404 Not Found
    nginx/1.9.3 (Ubuntu)

    I am able to access http://localhost which displays Nginx welcome message. Could you kindly assist me on this?

  7. Hi there,

    I am trying to follow this tutorial and use CodeIgniter 3.1.3
    But when I try the following url, I get a 404 not found error:
    http://testcase:8888/admin

    When I remove the /admin I get the default welcome page so that works.

    Could you update the whole tutorial, so it works on version 3.1.3?

    Thanks,

  8. Salut,

    Et merci pour cet excellent tutorial. J’ai suivi l’installation complète de la zone sécurisée l’admin.
    Je peux maintenant administrer les groupes, utilisateurs qui sont enregistrés dans la base de données.

    J’essaye de construire un module de news avec un champ d’upload mais le user guide de CI ne travaille pas avec les render-> (et le My_Controller). Comment puis passer les variables dans les render-> ?

    Peux-tu me mettre sur la voie ? Merci.

  9. Thanx for sharing. Just One question. Do you think this approach is secure? I’m thinking of restricting routes that start with admin only for logged in users with role admin? what u think?

Leave a Reply

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

No spam? * Time limit is exhausted. Please reload CAPTCHA.