Create an Admin panel with CodeIgniter

Posted: 2009-07-08
Category: CodeIgniter

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
dashboard.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. You'll need to set a $route['admin'] = 'admin/dashboard'; to get example.com/admin worksing but thats easy enough.

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

Gravatar
Webmasterdubai

2009-07-08

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.

Gravatar
Mei

2009-07-08

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

2009-07-15

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
Sharyn Proctor

2009-08-18

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
Shailesh

2009-10-08

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
Udi Mosayev

2009-10-27

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
Jay

2009-11-05

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
Unni

2010-01-08

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

Gravatar

2010-02-10

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
Umefarooq

2010-02-17

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
Viral Mehta

2010-03-27

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
Adem

2010-03-27

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
Taner Ozdas

2010-03-29

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
Vikry

2010-03-29

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

Gravatar
Sangoku

2010-05-04

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

2010-05-05

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
Nicolas

2010-05-21

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
Maderic

2010-06-02

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
Nilesh

2010-06-02

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

Gravatar
Dairon Medina

2010-06-29

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

2010-06-30

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
Janos

2010-07-06

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
Erik

2010-07-21

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
J

2010-07-30

Hi,

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

Thanks
J

Gravatar
Pankaj

2010-08-09

thank you so much i learned alots to you third method, actuallty i am new of codeIgniter so i am trying to make admin panel so if you have more information abt this then please help me.
i understand something how is working function

best regards
pankaj singh

Gravatar
Francois

2010-09-01

"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 would need some clarification on that statement. At first you say it's weird, and then you use exactly this method. And that's the one you recommend in the summary.

I don't have specific question, I just felt a little lost while reading that part, could you explain it a little bit further ? Is it good or not ? Why does it go against the point of using module ? (You can remove and add a folder right away). Why you're recommending this for big development ?

Thanks, and nice article !

Gravatar

2010-09-01

Sangoku : I can see where the confusion is coming from there. Having a blog module and comment module makes sense, having an admin module does not.

Gravatar
Jnik

2010-09-28

Thanks for the article, I was looking on a way to separate the control panel from the rest of the site with CI.

I have been separating the business logic from the presentation logic (smarty) for some time now and I'm looking into taking things one step further and adopt to the MVC way of coding. The main reason for that is to increase the re-usability of my code, but with custom made websites/e-shops I don't thing that will be the case. I see how I could reuse the "news", "contact us" and some simple pages models and views, but when it comes to products and product details things might be very different from one e-shop to another. If I have to edit the models for every project then that doesn't change much from what I'm doing now.

Any thoughts on that, or maybe some online articles about MVC and if it's suitable for smaller custom e-shops, would be very appreciated.

Gravatar
Swapnil Kene

2010-10-21

Actually the #3 method can be implemented in different way as well which will be more promising. You can place modules dir outside system dir. url will then be www.exmp.com/admin/ which is again same. So it will just help simplifying HMVC Structure.

Gravatar
Brando Bandit

2010-10-24

Thanks for your post Phil! I have initially tried the 1st method but have no idea how will I access the admin panel in the address bar? I tried http://mysite.com/index.php/admin but it is not working. Can you please enlighten me?

Gravatar
Chamil

2010-11-02

I also have same problem which Brando Bandit have.
my error is admin/index.php on line 115
please help me ASP.

Gravatar
Chamil

2010-11-02

Ohh thank CI I solve the problem
Put $system_folder = "../system"; these in admin/index.php line 26

Gravatar
Andy

2010-11-08

How would you implement method 3 using the newer Modular Extensions - HMVC? For example where would the main admin controller that acts as the default admin page be in the newer Modular Extensions - HMVC?

Gravatar
Paul

2010-12-04

Hi guys!
I've been working for a while with CI but now I'm planning a bigger application and I want to learn more about HMVC.
Did anyone tied the third method Phil posted here in a real application?
Can anyone explain me how it works or to point me to a tutorial or something?
Eg:
-what is "admin" doing in the application/controllers ?
-what are the other "admin" doing in the modules/xxxx/controllers ?

I would really appreciate your help!
Paul

Gravatar
Kai_desu

2010-12-07

Paul, both of those questions were explained in Phil's original post.

The admin controller found in "application/controllers" would be your main admin page (eg. www.yourdomain.com/admin).

The other admin controllers found in the modules folder(s) serve as the admin controllers for that particular module. Without routing, your URL would like "www.yourdomain.com/news/admin"

But with a little routing magic, you can make the URL look like "www.yourdomain.com/admin/news"

This way, you have clean, linear URLs while maintaining a modular setup.


Hope this cleared things up for you.

Gravatar
Paul

2010-12-20

Thanks kai_desu for your reply!
I have another question:
How can I create static pages like Home, About us, Contact?
Do I have to create different modules or new controllers in application/controllers/home (about, contact) and their views in application/views/home_view (about_view, contact_view)?

Happy coding everyone!

Gravatar
Ossama Khayat

2010-12-31

Great article. I used method #2 for my first application. Was afraid to use method #3 (modules) as the default CI doesn't yet implement, and was in a hurry. But surely considering to use it for future applications.

Gravatar
Suranga

2011-01-04

Hi, Realy useful artical, I ve been looking for this for weeks now and I almost got fed up.
Thanks

Gravatar
Sein Oxygen

2011-01-17

Thank you so much, this helped me a lot.

Gravatar
Dvbxyz

2011-02-02

Hello.

Thank you for this article. It is really interesting.

I have a small question about the third method.
i'm trying to implement it with the Internationalization (i18n) library and i cannot make the rewrite route for the admin working.

currently my address pattern look like mysite.com/en/controller except for the admin where i have an exception ( mysite.com/admin ) - no localization

the route i'm using are the following:

$route['^fr/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
$route['^fr$'] = $route['default_controller'];
$route['^en$'] = $route['default_controller'];


if i had
$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';


mysite.com/en/blog/admin is working,
mysite.com/blog/admin is working, (redirect to mysite.com/en/blog/admin)
but mysite.com/admin/blog is not.


I don't see what i make wrong


Thank you.



Gravatar
Ryantran

2011-03-02

hey...thank u very much ! i done ! :D

Gravatar
Thomas

2011-05-06

hello phil! hello ervery body i am using the third choice now. but i stuck in set the routes for it
this is the basic idea for switch from pages/admin to admin/pages
$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';

how about if my admin(controller) have function to reset password and it passing the forgotten code.
so its gonna be
admin/reset_password/3a7dya8dwdh82y8dbahd8ada
the routing is end up with 404

thankyou

Gravatar
Jayesh

2011-05-31

I was looking for the same stuff and the second method looks promising. The same method is mentioned on CodeIgniter tutorial's controller section. Anyway good post, congrats and many thanks.

Gravatar
Kiran

2011-06-16

I have tried to used second method that is creating sub folder inside controller but there I am a lot of problem like redirecting problem. Such as when redirect from constructor of admin's file say dashboard.php using redirect('dashboard/login'), it says, page redirection is not correct.
I have used another method also like redirect('admin/dashboard/login'), it also says same thing. But this method works when redirecting from dashboard's other methods.
Also another problem is that I am not able to remove index.php while redirecting from admin page which I was able do it for frontend pages.
Please could you help me to get rid of this problem.
Than You.

Gravatar
Tunggul

2011-07-16

wonderfull.....thanks alot phil

Gravatar
Marco Monteiro

2011-08-10

Lovely! Currently using option 1... I'll give it a go on the other ways to do this on my next project..

Gravatar
Donelle Galanga

2011-08-20

Hello Phil, Thanks for this article.. I tried to use the 2nd option and its seems its not working in version 2.0.2

Gravatar
Raf

2011-09-29

Hi,

I was wondering about using 3rd option with Modules within Modules. In such case, I had in mind to set the route as following. However it is not working.

real url: http://website.com/admin/locations/places/1
$route['admin/([a-zA-Z_-]+)/([a-zA-Z_-]+)/(:any)'] = '$1/$2/admin/$3';
real url: http://website.com/admin/locations/places/
$route['admin/([a-zA-Z_-]+)/([a-zA-Z_-]+)/'] = '$1/$2/admin/index';

Gravatar

2011-09-29

What are modules within modules? I wrote some sub-module code for a random client once but it's not been released and I doubt this is the sort of thing your are talking about.

You need to do more than just "make a folder and make a route" to create sub-modules, of course it is 404ing.

Gravatar
Raf

2011-09-29

Yeah it is about sub modules. I guess I will need to extend the loader with extendable routes defined in each module, something like what is been done in PyroCMS. Thanks for the support. :D

Gravatar
Andrey

2011-12-05

Article is very helpful, thank, but I was actually looking for an article that will tell how you can "code" admin panel using codeigniter.
And this gives you MUCH better idea about it:
http://andreytech.com/creating-crud-admin-panel-in-codeigniter/

Gravatar
Cipher Trace

2012-01-06

I have used all 3 methods.. and i found 1st one is better :)

Gravatar
Sp 6

2012-02-22

I have already built the frontend of the whole site and now the client wants few features to be added in an admin section. I followed the 1st method but it didn't work for me except the very first page (i.e. set in routes.php as $route['default_controller'] = "main";). The URL looks like "http://localhost/myproject/admin/dashboard" , yes it leads to a 404 page. I have fully followed the way you have described above (folder structure and setting 2 index files).
Am i missing anything? or it just doesn't work at all in my case?

Gravatar
Replica5rndbagswda

2012-03-08

Everybody wants an excellent 1st <a >replica handbags</a> purse or perhaps LV purse because of the fashionable, classy in addition to distinctive style and design. If you are spending earnings with an original LV bag or handbag, it is really crucial which you are not necessarily fooled by the retailer. Provided underneath are a handful of strategies along with tips may likely let you admit the gap among a reproduction having an initial Lv carrier. Just in case you intend getting the LV company through the greater important thing department retain or an LV wall socket, you then may be self-confident about the trustworthiness. These retailers may likely market merely authentic Lv items.
Few people may be qualified to acquire from your store shop. The higher daring assortment should acquire <a >replica louis vuitton handbags</a>food to determine an individual at an inexpensive and in addition cost-effective price. Whenever you analyze the internet, you had take place through plenty of on the net vendors whom motivate these purses and handbags and also vast majority come out fake .. Several of the entries are actually interesting it is in fact hands to overlook. Make sure to tackle this list and acknowledge a few factors prior to getting your own buy.
Just before getting virtually any designer brand name bag, it is in reality substantial to know the hardware of those specific brand name. For a lot more information, you could search the particular Lv web site as well as fork out a chance to have the ability to about the best LV go shopping where you will manage to funds more details. Explore for your day distinctive codes or perhaps in <a >Buy louis vuitton</a>the marriage the manufacturer title functions by making use of almost any distinctive selection such as feet or maybe whether or not they`ve the layer made from any kind of content. Such as the LV fast usually provides lightly browning organic cotton. A great trustworthy LV handbag or even purse has not ever before made use of dark brown suede liner. Actually their old-fashioned parts never have utilized dark brown textures. Look into pertaining to fridge brings, zip fasteners, buckles together with handbag photos. Each one of these may have your own LV company logo in their mind. Zip drags relating to LV can be found in brass products along with likewise the completely feel and also effect can result in considerable. LV can be excellence personified each handbag as well as purse will be made by hand. Plenty of interest covers the maintaining their own symbol from your handbag. Hence in case you discover that the actual LV may be relatively unequal, it's in fact definitely the handbag is usually a look-alike or maybe bit of junk. The brand LVs are positioned fantastically to the carrier. You may make utilization of any ruler to see for those by yourself the job from the LVs.
Everybody cannot afford to have these kind of significantly costly manufacturers, consequently their unique reproductions provide you with the manner aware individuals with what they desire. It's not at this time required to have funds when you wish to hold absent your self in fashion and elegance. The specific reproductions of the <a >cheap replica handbags</a>purses have given individuals who have wonderful possibilities to pick the issues that are usually well-liked around the globe. The actual copies offer the exact same appearance and really feel along with will be attracting individuals closer inside equal method. They could be truly created for many who end up keen on moving the actual top quality as well as other fashion affirmation items using these yet because of their through the roof expenses, they could be battling to do this.
Prior to searching for any custom model bag, it's the truth is significant to grasp the particular components from the exact name. For a lot more details, you could flick through the specific Louis Vuitton web site or spend a go to be able to in regards to the local LV shop through which you will be capable of funds more details. Take a look at for the night out rules or perhaps in the wedding the business name functions utilizing almost any distinctive sort just like toes or even no matter whether they`ve any cellular lining made from any specific material. Including the LV quick often has gently lightly browning 100 % natural cotton. <a >wholesale replica handbags</a>An excellent trustworthy LV tote or possibly bag has not in fact made use of darkish suede cellular lining. Actually his or her old-fashioned components have not adopted brownish textures. Investigate regarding fridge drags, zip fasteners, buckles as well as bag photographs. Most of these may have the LV company logo in it. Zip brings including LV are also available in metal computer systems as well as likewise the particular completely really feel as well as touch will probably be big. LV will probably be perfection personified each and every tote or perhaps tote is really homemade. Plenty of acknowledgement pays off toward maintaining of his or her logo design inside females bag. Thus in the event you discover the actual LV may be a bit spectacular, it is in reality undoubtedly the tote is usually a imitation or possibly piece of junk. The logo LVs take a seat magnificently for your carrier. It is possible to utilize a head to check out for a lot of by yourself the task inside the LVs.

Gravatar
Mehmet

2012-06-10

how can I use modules::run command for admin controllers ? ( use with 2.) Sub-directories )

Gravatar
Flo

2012-06-13

I need some help with HMVC instalation for backend in 1(Two applications). HMVC modularization seams to work only for the frontend. Tk;s

Posting comments after three months has been disabled.