Phil Sturgeon

Web developer, kayaker, outdoors madman and part-time alcoholic.


Create an Admin panel with CodeIgniter

Posted CodeIgniter at Jul 08, 2009

As I see it there are three methods to creating an admin system using the MVC framework CodeIgniter. In this article I will show examples of the structures for each and mention the pro's and con's of using each.

This article will only outline the theory and suggest the structures to you. I do not plan on writing yet another "How to make a user login system and add admins to it" type article.

1.) Two applications

In CodeIgniter you can easily set up multiple applications to run off the same CodeIgniter install, simply by creating a copy of index.php and renaming it to something else.

/
applications/
frontend/
controllers/
home.php
blog.php
comments.php
models/
blog_model.php
comment_model.php
views/
blogs/
index.php
view.php
comment/
view.php
index.php

backend/
config/
controllers/
dashboard.php
blog.php
comments.php
models/
blog_model.php
comment_model.php
views/
blogs/
index.php
form.php
comment/
index.php
dashboard.php
login.php

system/

index.php
admin/
index.php

Here you can see I have put index.php into an admin/ folder of its own. Both index.php files will point to a single folder within /applications and this can be done by setting:

index.php

$application_folder = "applications/frontend";

admin/index.php

$application_folder = "applications/backend";

This method does work, but is only really any good for big sites that have very different content for their front and back ends. You cannot use the same libraries, helpers, models, etc which will mean its very easy to end up with duplicated code. I'm not a big fan of such frontend/backend separation as for most sites, an admin panel will use the same models and code but this varies entirely on the spec of the site you are building.

2.) Sub-directories

This method follows a more usual CodeIgniter set-up and is the way that most new CodeIgniter users will try things at first.

/
application/
config/
controllers/
admin/
blog.php
comments.php
blog.php
comments.php
models/
blog_model.php
comments_model.php
views/
admin/
blog/
index.php
form.php
comments/
index.php
form.php
dashboard.php
login.php
blog/
index.php
view.php
comments/
view.php
system/
index.php

Here we are keeping the default MVC structure of CodeIgniter and using sub-directories for our controllers to give us the http://example.com/admin/blog URL structure. This method has the advantage of being able to share models, libraries and helpers across both the front and backend. If you really need to separate models for front and back ends, why not just have a models/admin/ folder and put them in there?

The down side is that when your site expands and more controllers are required, it can be a real pain to have your content so loosely linked across the entire application directory. You can see in the example above that we have several folders for blog and comment content, where really we should only have one. This one folder is called a module...

3.) Modules

To keep all the content under one single folder we can adopt the HMVC approach. This stands for Hierarchal MVC which essentially is just modular CodeIgniter. Two systems have been developed for this: HMVC and Matchbox. I personally prefer use the latter but have never tried HMVC so i'll leave that one up to you.

A strange thing that many CodeIgniter users seem to do is create a blog module, comment module and admin module. This strikes me as a very strange separation of content that goes against the point of using modules in the first place! I have a single admin.php controller in the main controllers folder to act as the default admin page which will handle login, logout and the main dashboard. Then I add another admin.php controller in each module and use URI Routing to get my URL structure as http://example.com/admin/.

/
application/
config/
controllers/
admin.php
modules/
blog/
controllers/
admin.php
blog.php
models/
blog_model.php
views/
admin/
index.php
form.php
comments/
controllers/
admin.php
comments.php
models/
comment_model.php
views/
admin/
index.php
form.php
views/
admin/
dashboard.php
login.php
system/
index.php

Right now to get at the blog admin you would have to go to http://example.com/blog/admin which may well be fine with you. If not, you can add the following routes to your application/config/routes.php to swap the segments around and emulate a /admin/ folder.

$route['admin/([a-zA-Z_-]+)/(:any)'] = '$1/admin/$2';
$route['admin/login'] = 'admin/login';
$route['admin/logout'] = 'admin/logout';
$route['admin/([a-zA-Z_-]+)'] = '$1/admin/index';
$route['admin'] = 'admin';

This way you have your admin controllers kept with the frontend controllers, you are sharing models, libraries and helpers and you still have some nice URL's.

Summary

If your front and back end applications share nothing in common and never will do, use method #1. If you have a small site with one a few controllers and do not want the small overhead HMVC adds, use method #2. If you are working on a massive site that is modular and shares code between front and back ends, use method #3.

Got any more methods to handle admin structures? Let me know in the comments.


dashboard.php

Comments

User comments
  • Gravatar J

    Jul 30, 2010

    Hi,

    Thanks for writing the post. It's really a great help to the beginners in CI.
    Extremely helpful.

    Thanks
    J

  • Gravatar Erik

    Jul 21, 2010

    Hi Phil,

    Thank you for writing this article. I've just started my first project based on CodeIgniter and so far all is going pretty well. I'm trying to implement the 3rd method you described which seems the most 'modular' of all. However, I can't seem to get the custom routes working. It seems that the routes with regex patterns (switching segments) are ignored by the router somehow, as it falls back to the default CI router which doensn't support modules. Any thoughts on why this might happen? Thanks again.

  • Gravatar Janos

    Jul 06, 2010

    Thanks for the writeup Phil.

    I really like the separation of the backend from the frontend which the second method provides, while providing easy access to the same controllers, models, utilities, etc.

    I have a small tip for all those wishing to use this method (as I got tricked at first try):

    The $route['default_controller'] = "home"; line in your routes configuration file influences the contents of the admin folder too. For example in this case when you go to site.com/admin you better have a controller named home in your admin folder or else unexpected stuff will start happening.

  • Gravatar http://philsturgeon.co.uk/

    Jun 30, 2010

    Dairo: You can only load content from within your current application in CodeIgniter 1.7.2. You could try using "Application Packages" in CodeIgniter 2.0. Grab the code from BitBucket and look at their user guide (changelog) to see how Packages work.

    I'll probably write this method up as "Method 4".

  • Gravatar Dairon Medina

    Jun 29, 2010

    Hello i created 2 different application /app1 and /app1Admin. I like that app1Admin is able to load and manipulate config files from app1. Please help me fix that problem.

  • Gravatar Nilesh

    Jun 02, 2010

    very nice article,i am definatly going to use it in my project

  • Gravatar Maderic

    Jun 02, 2010

    Hi Phil, what do you think is the best way to organize files css, js and images within a module? and thus make more clear the reuse of code, greetings

  • Gravatar Nicolas

    May 21, 2010

    I have two questions if you're ok.

    In the first method, I added the htaccess to remove "index.php" and the admin does not work anymore -> 404. Why and what should I do?

    The second question is simple at first, but eventually I really do not find how to manage it.

    In the third method that I tested also, I faced a problem when using sub-sub folders.
    In the docs and examples, a module is a simple section in my case I have for example: module classifieds> categories, brands, etc. .. and after the methods for each controllers, I do not know how to handle this "sub-sub-folders" as it takes for a function ..

    Thank you for your help and clarification.

    ps: actually my first question is a kind of parade to achieve what I want.

  • Gravatar Phil Sturgeon

    May 05, 2010

    Sangoku: Well that depends on the way the site works, what the admin is doing, what sort of stuff is going on. As I see it, multi-site does not effect the structure of your code at all.

    The only difference multi-site makes is how you are detecting it and how you are splitting out your data, all of which can be done in the database.php or MY_Controller.php.

    I have been known to use a sub-directory for a "Super Admin" panel (this panel controls sites) then use Method #3 for a "Site Admin" backend (news, pages, etc) and the frontend stuff.

  • Gravatar Sangoku

    May 04, 2010

    Hello,

    What is the best option if you want 1 admin and multiple sites? The sites are using the same functionality, the admin can be separated from the sites.

  • Gravatar Vikry

    Mar 29, 2010

    thank you for sharing, I've searching for this methodes for several times, :)

  • Gravatar Taner Ozdas

    Mar 29, 2010

    Very detailed and helpfull information. I have a plan to use one of these technics at my project. Because project getting harder to maintanance , and it sould be more organized.
    Thank you for this post.

  • Gravatar Adem

    Mar 27, 2010

    pls help me i m new in codeigniter , how to create a page and install step by step and how to create a controller.

  • Gravatar Viral Mehta

    Mar 27, 2010

    I am still not able to understand your third option. can you explain you third example little bit in detail or a sample cod

  • Gravatar Umefarooq

    Feb 17, 2010

    Hi Phil
    for third method a little modification in route and file names

    $route['admin/([a-zA-Z_-]+)/(:any)'] = '$1/$1_admin/$2';

    if you have like this developer can understand which controller is open where only admin file name 3 or 4 files are open same name hard to understand which controller has to be modified or right now working on it

    news/admin will open news_admin file.

  • Gravatar Phil Sturgeon

    Feb 10, 2010

    Unni: It works exactly the same with Smarty as it would in any other layout... I would take a look at my Dwoo implementation in the Code section. Combined with my module aware Template library you should be making modular designer-friendly applications very easily.

  • Gravatar Unni

    Jan 08, 2010

    Nice post. How does it work with Smarty or any other templating system?

  • Gravatar Jay

    Nov 05, 2009

    I Like the 2nd option.
    Sub Folder is nice one.. But Can you explain more in brief.

    I have create sub folder Now what change is required?

    www.test.com/admin

  • Gravatar Udi Mosayev

    Oct 27, 2009

    Once I created an application with administration panel as a separate application and it was a disaster!

    Then I tried the module approach but little bit differently:
    insdie Controllers/Models/Views I created the Module folder.
    So instead
    Module
    Module/Controllers
    Module/Models
    Module/View

    Its
    Controllers/Module/...
    Controllers/Module2/...
    Models/Module/..

    and etc.
    Its really convenient for me, and looks organized

  • Gravatar Shailesh

    Oct 08, 2009

    pls help me i m new in codeigniter , how to create a page and install step by step and how to create a controller.

  • Gravatar Sharyn Proctor

    Aug 18, 2009

    Thanks so much for your help here :)
    I am learning and love to collect the info, my head spins, but it keeps me off the streets :)

  • Gravatar Phil Sturgeon

    Jul 15, 2009

    That is true Webmasterdubai but I like to keep everything modular when possible. Why had modules if you are adding module related content into an admin module?

    Pretty much defeats the point of using modulesi n the first place! :-)

  • Gravatar Mei

    Jul 08, 2009

    Good article.

    I've used method #2 on a recent site (still in rolling developement), but I think that #3 is probably the way to go. But I'll have to get my head round modules first!

  • Gravatar Webmasterdubai

    Jul 08, 2009

    in third method no need to change any thing in route.php if you create one admin folder and put all you controllers in admin/
    controller/admin.php,blog.php,content.php will also work nice.

Post a comment