Search code examples
codeignitermodulecodeigniter-2hmvc

Best technique for module implementation in Codeigniter


I am a little familiar with CI, and now I am trying to build a web portal using it. I am trying to make it flexible enough to accept widgets/modules. Just like how components are to joomla.

For this, (I think) I should create modules, but the sad part CI by default does not accept any modules. But through HMVC or Modular CI, even this might be possible, but as i haven't used them before, I do not know which would fit in my case, for the long run.

Basically, I would like to communicate with other modules and their controller through a common controller. Kind of like a front controller.

For example take my default controller as Site and the functionality of what I am looking is somewhat like this...

class Site extends CI_Controller {
    public function index() {
        $appName = $this -> uri -> segment(1); // Take this as app name
        $appControllerName = $this -> uri -> segment(2); // Take this as app controller name


        $this -> load -> module($appName); //Loading our app Module

        $this -> $appName -> load -> controller($appControllerName);

        $this -> $appName -> $appControllerName -> render(); 
        // Take Render() as one of the common method that all the modules controller should have and is reponsible for rendering the HTML

    }
}

Above code is just what I am trying to get. There might be better way to do these. Either way I am looking forward to your replies.....


Solution

  • User Controller

    //MX_Controller is the HMVC controller, so anything extending
    //this class is a Module
    
    class User extends MX_Controller{ 
    
    //Public function hidden from URL but accessed via Module
    
    public function _comments($user_id){ 
        //grab comments for this users from your database
        //return as an array or object
    } 
    }
    

    once inside your views you can access any number of modules...

    //Dashboard_view.php
    
    //Module One
    foreach( Modules::run('User/_comments', $user_id ) as $user_comments )
    {
       // return all comments for this user
    }
    
    //Module Two
    foreach( Modules::run('Widgets/_show_random_stuff', $user_id ) as $user_widgets )
    {
       // return all widgets for this user
    }