Create an Admin panel with CodeIgniter

Create an Admin panel with 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.

Related Posts


8 thoughts on “Create an Admin panel with CodeIgniter

  1. Beata Neugin

    Appreciating the time and energy you put into your website and detailed information you offer. It’s great to come across a blog every once in a while that isn’t the same unwanted rehashed information. Fantastic read! I’ve saved your site and I’m including your RSS feeds to my Google account.

    Reply
  2. kore dizileri izle

    Nice one weblog owner accomplishment webpage post great sharings with this blog site at all times have exciting

    Reply
  3. Lizzette

    I essential for this web site put up admin really thanks i will appear your upcoming sharings i bookmarked your blog site

    Reply
  4. Ward Karapetyan

    I just want to say I’m all new to blogging and site-building and honestly enjoyed this blog. Most likely I’m going to bookmark your blog . You really come with excellent articles. Many thanks for sharing with us your web page.

    Reply
  5. Form For Web

    Hello, Neat post. There is a problem together with your website in web explorer, could check this? IE still is the marketplace leader and a large component to other people will omit your excellent writing due to this problem.

    Reply
  6. blonde girls

    I have learn several good stuff here. Certainly price bookmarking for revisiting. I wonder how much effort you put to make this sort of wonderful informative site.

    Reply
  7. Accounting Translation Canada

    Hi there very nice web site!! Guy .. Excellent .. Amazing .. I will bookmark your web site and take the feeds additionally?I’m happy to seek out numerous useful information here in the submit, we’d like work out extra techniques on this regard, thanks for sharing. . . . . .

    Reply

Leave a Reply

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


*