naeplus
naeplus

Reputation: 121

Can i handle Codiegnitor admin and main website within same application?

actually i am looking to handle my website/admin panel withing same application. so i want to know that is that possible?

i means structure something like this
http://www.mysite.com
and for admin
http://www.mysite.com/admin

so this all i need to handle withing one application of codeignitor. i don't want two codeignitor installation for this purpose.

Upvotes: 1

Views: 147

Answers (2)

Chauhan Vipul
Chauhan Vipul

Reputation: 1

First 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 MY_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
        // Create and setup admin user session and other all dynamic admin url for image,js,css,etc..
    }
}

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

    function __construct()
    {
        parent::__construct();
        // Load any front-end only dependencies
        // Get user data of session and generate other all dynamic front url for image,js,css,etc..
    }
}

Back end controllers will extend Admin_Controller, and front end controllers will extend Front_Controller.Now You can create any admin side controller & models and extends to Admin_Controller and front side extends to Front_Controller.

E.g (Any Admin Controller) :

    class Admin extends Admin_Controller{
      function __construct(){
         parent::__construct();
      }   
    } 

E.g (Any Front Controller) :

    class Home extends Front_Controller{
      function __construct(){
         parent::__construct();
      }   
    } 

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

Upvotes: 0

Sarfraz
Sarfraz

Reputation: 382806

Sure, you can, see the section at CI docs which says:

Running Multiple Applications with one CodeIgniter Installation

You can also create separate folders for your controllers, models and views like:

+controllers
    +front (main site controllers will go here)
    +admin (admin controllers will go here)

+models
    +front (main site models will go here)
    +admin (admin models will go here)

+views
    +front (main site views will go here)
    +admin (admin views will go here)

See the section:

Organizing Your Controllers into Sub-folders

Upvotes: 3

Related Questions