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]
  • #76 / Feb 22, 2008 7:01pm

    wiredesignz

    2882 posts

    Thanks gerben 😉

    It really was a community effort, most of the ideas I read here were integrated. Thanks all.

    Has anyone had a chance to look at the HMVC class, I changed the way it uses the CI instance, is it slower now than when it had Sthis->ci ?

    Any thoughts appreciated.

  • #77 / Feb 22, 2008 9:12pm

    gerben

    75 posts

    Somehow the view library isn’t compatible with Modular HMVC, could that be? I’ve combined modular HMVC with Matchbox in the following way:

    A controller called “modules” loads my main layout (headers, footers, sidemenu, etc.) and it also loads a view that loads a Matchbox sub-controller for the main-content:

    modules::run($module.'/controllers/'.$module, $page, $method);

    So far so good, but the sub-controller (modules/blog/controllers/blog.php) goes into an infinite loop when I add this line to it:

    $this->view->load('posts'); // view library code for calling a view in modules/blog/views/posts.php

    But even the standard way of calling a view doesn’t work like it should work with Matchbox. I now have to call it like:

    $this->load->view('../modules/blog/views/posts');

    instead of the normal way:

    $this->load->view('posts');

    Did I do something wrong or something strange? Of course you haven’t created Matchbox, so maybe this question is a bit out of range. Anyway, it would be great if the two could be easily combined.

    Another small thing I noticed: when I call the sub-controller directly (through the url) I get the error:
    Fatal error: Class ‘HMVC’ not found in /Applications/MAMP/htdocs/trunk/application/modules/blog/controllers/blog.php on line 3

    It would be great if you could see the subcontroller’s output there. That way you would have one page containing different subcontrollers, which all can be reached through their own url! I don’t know if that’s possible, though.

    Apart from the views, it works just great! Thanx so much!

  • #78 / Feb 22, 2008 9:55pm

    Edemilson Lima

    241 posts

    Has anyone had a chance to look at the HMVC class, I changed the way it uses the CI instance, is it slower now than when it had Sthis->ci ?
    Any thoughts appreciated.

    Looking at some functions in Modules class, I noticed that the line “$ci = & get_instance()” is executed at each cycle of the foreach loop:

    foreach ($modules as $module => $config)
            {
                $class = substr($module, ($pos = strrpos($module, '/')) ? $pos + 1 : 0);
                
                $is_loaded = modules::_load_module($class, $path.substr($module, 0, - strlen($class)));
    
                $ci = & get_instance();
    
                if($is_loaded)
                {
                    $ci->$class = new $class($config);
                    $ci->load->_ci_assign_to_models();
                }    
            }

    Does this line need to be executed at each looping?

    To answer your question, I think call a function like in “$ci = & get_instance()” is a little bit slower than just reference a class property, like as “$this->ci”. But I think this is the price we must pay to have more flexibility. You should not care that much because the get_instance() function is very simple and small:

    function &get;_instance()
    {
        return CI_Base::get_instance();
    }
  • #79 / Feb 22, 2008 11:07pm

    wiredesignz

    2882 posts

    Good point Edemilson. I was moving that line of code around so often it ended up in a poor place. Thanks 😉

  • #80 / Feb 22, 2008 11:45pm

    wiredesignz

    2882 posts

    Current version is 2.1.1
    Rewitten the HMVC class definition.

    Thanks for your interest. 😉

  • #81 / Feb 23, 2008 12:17am

    Rick Jolly

    729 posts

    I think call a function like in “$ci = & get_instance()” is a little bit slower than just reference a class property, like as “$this->ci”.

    Getting a reference to an object costs nothing. Someone actually benchmarked $this->ci vs & get_instance() and I think & get_instance() was a tad faster over thousands of iterations. Either way, performance isn’t an issue.

    Edit: I found the thread by tonanbarbarian:
    http://ellislab.com/forums/viewthread/68826/
    This thread was actually dealing with memory usage as opposed to performance.

  • #82 / Feb 23, 2008 12:42am

    wiredesignz

    2882 posts

    Thanks for that explanation Rick. I prefer to use the local $ci instance so that helped. 😉

  • #83 / Feb 23, 2008 3:21am

    wiredesignz

    2882 posts

    Current Version is 2.1.3
    Modified HMVC class loader
    Added plugin loader
    Added modules::debug() method

  • #84 / Feb 23, 2008 8:49am

    wiredesignz

    2882 posts

    Now that I’ve started using the Modules Loader and the HMVC class (instead of developing it) its turning out to be a dream come true as far as productivity is concerned.

    Things I used to cram into a controller or library now fit perfectly in a module.

    Types of usage are pretty versatile beacause the loader returns the module object so you can re-assign it in the current scope:

    //all validation rules & fields setup is now modular
    
    $this->validation_setup = modules::load('users/validation/setup');   
    
    //the object would normally be named $this->setup

    Modules that don’t extend the HMVC class will provide static helper methods.

    Modules can be loaded and run with a single line of code, or loaded and then run later, with no conflict.

    And you gotta love modules::debug($this); - You can feed it any valid object and it displays a list of the currently assigned object variables.

    I have also discovered that you can assign a module to another object already existing in the CI super object.

    $this->users_model->validation->setup = modules::load('users/validation/setup');
    
    //adds the validation setup module into the model, and it works.


    I’m sure there are also many other ways to use the modules, It’s just up to your imagination.

    I really want to thank all the members that contributed to an awesome extension to CI.

    Cheers 😊

  • #85 / Feb 23, 2008 9:44am

    xwero

    4145 posts

    I’m glad you like it, wiredesignz put a lot of effort in it to make it match and overpower matchbox :lol:

  • #86 / Feb 23, 2008 11:11am

    gh0st

    130 posts

    Okay - I’ve got matchbox working, but I’ve deleted that for the moment and want to try this Modular HMVC, but am completely stumped on how to start.

    Here’s what I have.

    Controllers/test.php

    <?php
    class Test extends Controller {
    
        public function __construct()
        {
            parent::Controller();
        }
        
        function index()
        {
            // Load Module Helper
            $this->load->library("modules");
    
            // Loading modules in a Controller
            $this->modules->load('fighters');
        }
    }
    ?>

    When I try to run this, the fighters module never loads, I get this message;

    Unable to load the requested module: modules/Fighters

    My folder layout is thus;

    application
    > modules
    —> fighters
    ——-> controllers
    ——-> models
    ——-> views

  • #87 / Feb 23, 2008 12:51pm

    badgeek

    26 posts

    you should put fighters.php in application/modules/fighters.php

    class fighters extends HMVC
    {
        function fighters()
        {
            parent::HMVC();   
        }
    
        function test()
        {
            echo "test";
        }
    
    }

    on your controller test.php

    function index()
        {
            $this->load->helper('modules');
            modules::run('modtest',false,'test');
        }
  • #88 / Feb 23, 2008 5:31pm

    Edemilson Lima

    241 posts

    application
    > modules
    --> fighters
    -----> controllers
    -----> models
    -----> views

    As you had organized above, I suppose you want to use Modular HMVC as Matchbox do. But it is not intended to replace Matchbox. You will just have a new folder called “modules” in the applications folder, where you put your modules/partials. Modules are like controllers that you can load and run from any controller or view you have. By its way, each module can load its own models, views and even more modules. This can go on and on, building any hierarchy tree you need for your application. However, all models and views loaded by your modules stay in their original directories. You don’t put them among your modules.

  • #89 / Feb 23, 2008 6:03pm

    wiredesignz

    2882 posts

    Heres a sample of usage, the module below configures the validation setup for my login controller.

    Login controller:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    class Login extends Controller 
    {
        function Login()
        {
            parent::Controller();
            $this->load->model('users_model', 'users');
            modules::load('login/validation/setup');
        }
    ...

    The setup module:

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    
     class Setup extends Module    // login/validation/setup
     {
        function Setup()
        {        
            parent::Module();
            
            $this->validation->set_error_delimiters('', '');
        
            $this->validation->set_fields(array(
                'username' => 'Username',
                'password' => 'Password',
                'remember' => 'Remember'
            ));
            
            $this->validation->set_rules(array(
                'username'     => 'trim|required',
                'password'     => 'trim|required',
                'remember'  => ''
            ));
        }
     }


    Because the setup code is in the constructor it runs as soon as the module is loaded.

    Hope this helps.

  • #90 / Feb 23, 2008 6:05pm

    Edemilson Lima

    241 posts

    @Wiredesignz: Why are you using the “$ci = & get_instance()” in the run() method?

    function run($module, $data = '', $method = 'index')
        {
            $ci = & get_instance();
            $class = modules::load($module);
            return $class->$method($data);
        }

    I would like to better understand the internals of the class. Why are two classes “Modules” and “HMVC”? How they work together? I did look the code but some things are a bit confuse to me…

    Personally, I would like to use “class XYZ extends Modules” instead of “class XYZ extends HMVC”... Are two classes really necessary?

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

ExpressionEngine News!

#eecms, #events, #releases