ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Modular Extensions - (HMVC) - Version 2.1.8

February 20, 2008 12:02am

Subscribe [6]
  • #31 / Feb 21, 2008 9:40am

    gerben

    75 posts

    Wow, this is an amazing addition to CI, wiredesignz! It’s something I (and I’m sure many other CI-users) have been waiting for!

    One question though: I would really like to use your library together with Matchbox, but in Matchbox controllers are placed in modules/module_name/controllers/controller_name. How can I call these controllers? I tried:

    <?php $this->modules->run('test/controllers/test'); ?>

    But of course it tries to load a controller called “controllers”, so that doesn’t work. Is there an easy way to acomplish this (maybe edit the in_subdir method?)

    Another question:
    If I understand it correctly, you can only render one method per module-controller (the index method). Would it also be possible to render more controller methods of the same controller as partials, e.g. render the index method, the sidemenu method and the footer method of the same controller. I hope you understand what I mean…

    Anyway, this is going to make CI even more enjoyable!

  • #32 / Feb 21, 2008 10:49am

    Sam Dark

    242 posts

    @gerben: why do you need this? Provide an example please.

  • #33 / Feb 21, 2008 11:13am

    gerben

    75 posts

    Let’s say I have a menu_items controller, which generates partials to be used in the main view: an ajax search bar, a list of newsposts, a poll and some dynamically generated links, then it would be convenient to be able to have all these sub-controller methods in one controller. This approach would keep your directory-structure cleaner, and you’d have all the menu_items code in one controller. It’s not necessary, it would just be a nice way to organize your code, and have less controllers lying around 😉

  • #34 / Feb 21, 2008 11:57am

    Edemilson Lima

    241 posts

    Would it also be possible to render more controller methods of the same controller as partials, e.g. render the index method, the sidemenu method and the footer method of the same controller. I hope you understand what I mean.

    This could be achieved with a little modification in the run() function:

    function run($module, $data = FALSE, $config = FALSE, $method='index')
    {
      $this->load($module, $config);
      list($class) = $this->in_subdir($module);
      return $this->ci->$class->$method($data);
    }

    But if you use the run() function to you load your modules directly from your views, I think you don’t need to run another method. For example:

    // main view:
    <div id="header"><?=$this->modules->run('header_module')?></div>
    <div id="contents"><?=$this->modules->run('contents_module')?></div>
    <div id="footer"><?=$this->modules->run('footer_module')?></div>
    
    // header module:
    class Header_module extends HMVC {
      
      function index() {
        $this->load->view('header_view');
      }
    }
    
    // contents module:
    class Contents_module extends HMVC {
    
      function index() {
        $this->load->view('contents_view');
      }
    }
    
    // contents view:
    <div id="offers"><?=$this->modules->run('offers_module')?></div>
    <div id="news"><?=$this->modules->run('news_module')?></div>
    
    // offers module:
    class Offers_module extends HMVC {
    
      function index() {
        $this->load->model('products_model','products');
        $data['offers_list']=$this->products->get_offers();
        $this->load->view('offers_view',$data);
      }
    }
    
    // news module:
    class News_module extends HMVC {
    
      function index() {
        $this->load->model('news_model','news');
        $data['news_list']=$this->news->get_last_news();
        $this->load->view('news_view',$data);
      }
    }

    But… You may want to have an add(), edit() or delete() methods in your News_module to manipulate the news. To execute these functions, you need to use the _remap() function in the controller, as Wiredesignz said. Another way to do this is to create a separated controller, instead of add new functions to your module. I think is better leave the modules only to render partials, not to act as controllers. You can do it, but is more easy to work with controllers, which natively grabs the parameters from the URI, than remap the URI to your modules. By its turn, the controller that will manipulate the data can load another modules, more suited for its needs. This is more flexible and is the right thing to do in a MVC structure.

  • #35 / Feb 21, 2008 12:21pm

    gerben

    75 posts

    Thanx a lot, I’ll look into these possibilities! I believe the _remap-function would be the best solution in my case, because that’s what I wanted to achieve: having the add(), edit(); and delete(); methods in one controller. Especially because I’m interested in integrating Matchbox with this library, and all my matchbox controllers are set-up like this. But maybe I should try to keep things simple at first 😉

    Any ideas on my other question on how to call a matchbox controller using this->modules->run()? Modular HMVC & Matchbox, that would make a killer combination!

  • #36 / Feb 21, 2008 12:57pm

    Edemilson Lima

    241 posts

    that’s what I wanted to achieve: having the add(), edit(); and delete(); methods in one controller.

    But you can have them in one controller. What I am saying is do not put the methods that will manipulate the data into the modules that will render the page. They can be placed in the same caller controller or another controller. Your controler can call the main view and pass to it what view will be loaded at the page center. The modules around it (ie. the header and footer) will be automatically called by the main view. This controller can have the add(), edit() and delete() methods. When any of these methods are called, the controller must load the main view and pass to it what related view (or even a module) will be loaded in the center of the page.

  • #37 / Feb 21, 2008 2:18pm

    gerben

    75 posts

    I was thinking more along the lines of a widgetized approach, where every sub-controller has its own add(), edit() and delete methods. But the add and edit screens of my widgets are loaded through ajax anyway, so it’s not a real problem.

  • #38 / Feb 21, 2008 10:38pm

    wiredesignz

    2882 posts

    Please notice I have updated the code. Version is 1.2.7

    Update: Improved the introduction comment block
    Update: Improved the run() and load() methods (see intro)
    Update: Removed the in_subdir() helper - now redundant

    Thanks for your interest. 😉

  • #39 / Feb 21, 2008 10:44pm

    Edemilson Lima

    241 posts

    I think the run() function could allow us to execute any method, but keep index() as default:

    function run($module, $data = FALSE, $method='index')
    {
      $class = $this->load($module);
      return $this->ci->$class->$method($data);
    }

    Is more flexible this way, isn’t it? 😊
    This library is becoming outstanding!

  • #40 / Feb 21, 2008 10:57pm

    wiredesignz

    2882 posts

    Current version is now 1.2.8
    Update: Added $method option to modules->run(). Thanks Edemilson Lima.

  • #41 / Feb 22, 2008 2:46am

    wiredesignz

    2882 posts

    Current version is 1.2.9
    Update: Added load->helper() to HMVC class

  • #42 / Feb 22, 2008 3:10am

    gh0st

    130 posts

    What are modules, why should I use them?
    What is Modular HMVC, why should I use it?
    Is Modular HMVC the same as matchbox?

  • #43 / Feb 22, 2008 3:40am

    wiredesignz

    2882 posts

    Read the two threads entitled `Nested MVC` and `Modular HMVC - Libraries on Steroids` respectively for an explanation.
    Search the net about HMVC, decide for yourself.
    While they both introduce modules as a concept, Matchbox operates differently, my system is able to work in Matchbox modules.

  • #44 / Feb 22, 2008 3:42am

    xwero

    4145 posts

    What are modules, why should I use them?

    Instead of having the models, controllers and views in a general directory and making subdirectories for files that belong together, modules have controllers, models and views in a separate directory. This makes maintenance for big or modular apps easier.

    What is Modular HMVC, why should I use it?

    It makes maintenance easier but it also makes it easier to create pages that are build out of many partials/widgets.
    If you have a basic site with a front-end and admin where you can only manipulate the data you can do it the way you are used to. Modular HMVC really shows its power used on portal sites.

    Is Modular HMVC the same as matchbox?

    It is the same as matchbox as they both break out the general directories but the main difference is matchbox alters CIs core where Modular HMVC is placed on top of CI.

    Wiredesignz continued effort making the library better than it already is, is incredible.

  • #45 / Feb 22, 2008 3:46am

    wiredesignz

    2882 posts

    Thanks xwero, your comment actually means a lot. 😉

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases