(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.
Great work done….
Thank you. I hope you’ve enjoyed it.
I follow this method now what will be the admin URL? I can’t run admin dashboard..
http://[yourdomain]/admin
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)
Thank you. Too much information is punishing me…
It still gives me a problem.
is it something with .htaccess?
or because its in a different map on my server ?
Not Found
The requested URL http://localhost/cms/admin was not found on this server.
If you type localhost does it return anything?
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
Tu peux parler aussi in francais… Pardonne-moi. Tu dois creer le fichier dans “application/controllers/admin”.
thanks.
It’s the same for the next pages, we should understand :
not: application/controllers/Dashboard.php
but—> application/controllers/admin/Dashboard.php
Yes. Sorry about that. I will correct it as soon as possible. Thank you for noticing.
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é
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.
Thank’s,
I forget the index.php for http://localhost/index.php/admin. Now it’s no problem. thanks
You can remove index.php from your URLs: http://avenir.ro/codeigniter-tutorials/removing-the-index-php-from-the-url-and-allow-the-use-of-search-engine-friendly-urls/
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
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 🙂
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
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?
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?
Hello. Make sure you’ve set up correctly the session (in application/config/config.php). If you want to use database for sessions, you can take a look at this tutorial: http://avenir.ro/codeigniter-tutorials/sessions-with-database-in-codeigniter/ Also, if you decide to use file driver for sessions, take a look here: http://www.codeigniter.com/user_guide/libraries/sessions.html
sorry, fixed by making below changes
$config[‘sess_save_path’] = FCPATH.’sessions’;
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?
Does it work when you visit http://localhost/index.php/admin ? If it works, then you didn’t remove index.php from your url (http://avenir.ro/codeigniter-tutorials/removing-the-index-php-from-the-url-and-allow-the-use-of-search-engine-friendly-urls/)
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,
It does work with CI 3.1.3. You just didn’t create an .htaccess file in order to remove index.php from url. Does http://testcase:8888/index.php/admin work?
Thank you, Best Tutorial Ever
Good job. Your blog helped me a lot. I did choose a gift in your wishlist. Enjoy! I play guitar with RockSmith. It’s nice.
Hi,
wonderful stuff,
please guide how to use datepicker in admin panel
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.
My project directory in core path I don’t have any file name MY_Controller.php. Kindly help with this.
Did you create MY_Controller.php file in “application/core” directory, as the tutorial says?
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?