Separate Admin and Front in codeigniter

Sohail picture Sohail · Sep 22, 2011 · Viewed 40.3k times · Source

What is the best way to separate admin and front-end for a website in codeigniter where as I was to use all libraries, models, helpers etc. in common, but only controllers and Views will be separate.

I want a more proper way, up for performance, simplicity, and sharing models and libraries etc.

Answer

Wesley Murch picture Wesley Murch · Sep 22, 2011

I highly suggest reading the methods outlined in this article by CI dev Phil Sturgeon:

http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter

My advice: Use modules for organizing your project.

https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home

Create a base controller for the front and/or backend. Something like this:

// core/MY_Controller.php
/**
 * Base Controller
 * 
 */ 
class MY_Controller extends CI_Controller {
                      // or MX_Controller if you use HMVC, linked above
    function __construct()
    {
        parent::__construct();
        // Load shared resources here or in autoload.php
    }
}

/**
 * Back end Controller
 * 
 */ 
class Admin_Controller extends MY_Controller {

    function __construct()
    {
        parent::__construct();
        // Check login, load back end dependencies
    }
}

/**
 * Default Front-end Controller
 * 
 */ 
class Public_Controller extends MY_Controller {

    function __construct()
    {
        parent::__construct();
        // Load any front-end only dependencies
    }
}

Back end controllers will extend Admin_Controller, and front end controllers will extend Public_Controller. The front end base controller is not really necessary, but there as an example, and can be useful. You can extend MY_Controller instead if you want.

Use URI routing where needed, and create separate controllers for your front end and back end. All helpers, classes, models etc. can be shared if both the front and back end controllers live in the same application.