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.

Nested MVC

January 31, 2008 9:08pm

Subscribe [14]
  • #61 / Feb 15, 2008 2:54pm

    BizComputing

    60 posts

    It’s all a matter of personal choice.  I choose to seperate out my fake controllers from my libraries and real controllers, hence my biz_loader, but it’s not necessary.  Seems I’ve drawn a lot of criticism for producing something that’s not necessary.  Oh well, should teach me to keep my mouth shut, but, I’m too thick headed to get the message.

  • #62 / Feb 15, 2008 3:02pm

    codex

    332 posts

    Seems I’ve drawn a lot of criticism for producing something that’s not necessary.  Oh well, should teach me to keep my mouth shut, but, I’m too thick headed to get the message.

    Well, not from me. I for one are happy that you are trying to tackle this issue. It’s something that I would like to have in my framework, but I lack the skills (and insight) to do so myself.

    So keep up the good work and let those criticasters talk. (Yeah, you know who you are 😛)

  • #63 / Feb 15, 2008 3:06pm

    BizComputing

    60 posts

    tkx

    now if I could just solve world hunger and end war…

  • #64 / Feb 15, 2008 3:09pm

    codex

    332 posts

    tkx

    now if I could just solve world hunger and end war…

    Just take one step at a time. You’ll get there eventually 😉

  • #65 / Feb 15, 2008 3:18pm

    Sam Dark

    242 posts

    Seems I’ve drawn a lot of criticism for producing something that’s not necessary.  Oh well, should teach me to keep my mouth shut, but, I’m too thick headed to get the message.

    You produced a very good and useful thing. I’ve just produced almost the same before looking at the forums. It’s always good to see someone is doing the same way you are. So thank you for the contribution.

  • #66 / Feb 15, 2008 3:52pm

    Edemilson Lima

    241 posts

    Seems I’ve drawn a lot of criticism for producing something that’s not necessary.  Oh well, should teach me to keep my mouth shut, but, I’m too thick headed to get the message.

    “When all men think alike, no one thinks very much.” (Walter Lippmann)

    “I cannot give you the formula for success, but I can give you the formula for failure, which is: try to please everybody.” (Herbert B. Swope)

    Don’t worry about the criticism, man. 😊
    If everybody agrees and think the same, we would be yet in the caveman age.

  • #67 / Feb 18, 2008 11:09pm

    Edemilson Lima

    241 posts

    Rick Jolly shows me a good way to load my partials as libraries or plugins, using a helper function to do that. In addition, he did convince me about the advantages of load the partials directly from the view, instead of the controller. A view centric is decoupled from the controler, which is much more flexible and easy.

    BizComputing did create another loader method to load my partials not from a library, but from a new folder, “modules”, that serve to better organize our files, with the advantage of use classes and not only functions.

    WireDesignz told that another loader method is unnecessary, since CI already have methods to achieve this using libraries, plugins or helpers.

    I was thinking about these opinions and about that in CI only one controller can run at a time. Thinking further, the user only interact with one controller at a time, because the web page only allows one form to be submitted at a time (unless you use AJAX in some special way). However, most of the time, we don’t need multiple interaction with user and the server. So, why we need a nested MVC anyway?

    Our main concern here is to simplify our partials rendering. In complex web page layouts, normally we have one active partial, where the user is interacting (generally the bigger area in the middle of the page). The other partials are mostly passive, they only display data.

    I was thinking: why do we need classes for our partials? I don’t think we even need functions for them. Think about it. You only need a script file that loads models and views, maybe some helpers and libraries. Well, you can create functions and classes in a script if you want, but the point is: where is really necessary to extend them from the main controller?

    Thinking about the WireDesignz opinion, I was looking at the code of the Loader library, and I noticed that the load->helper() and load->library() was designed to load a file only once. This is useful to load files with functions that shouldn’t be loaded twice. I think load->helper don’t server the purpose of loading a partial, because in a layout we may need to load (and execute) the same partial more than once.

    The load->plugin() and load->script() will load a file more than once. If you plan to use classes or functions, it will not work as expected. But load->script() is deprecated. It is almos the same of load->plugin(), except that load->plugin() will check in the application folder, and them the plugins folder under system folder.

    The load->file() is a loader for a generic PHP file. It works almost the same way of load->view(). It executes the script, store the result buffer and return it, but do not extract vars as load->view() does. May this function is a good option to load our partials, as it can load any script we need. The only issue is passing to it the instance of the CI object. May our helper function could do this for us, if we extend the core and made our own $this->load->partial().

    Examples:

    // example controller:
    Some_controller extends Controller {
    
      function Some_controller() {
        parent::Controller();
        $this->load->model('model_file','model_name');
        $data['page_title']='Title of this page';
        $data['username']=$this->session->userdata('username');
        $this->load->vars($data);
      }
    
      function index() { // for example, a list of products
        // do something
        $data['page_title']='Title of this page - index';
        $data['content']=$load->view('some_view1',$any_data);
        $any_data['center_page']=$this->model_name->function();
        $this->load->view('main_view',$data);
      }
      
      function action1($id) { // for example, a form to add a product
        // do something
        $data['page_title']='Title of this page - action1';
        $any_data['center_page']=$this->model_name->function2();
        $data['content']=$load->view('some_view2',$any_data);
        $this->load->view('main_view',$data);
      }
      
      function action2($id) { // for example, a form to edit a product
        // do something
        $any_data['page_title']='Title of this page - action2';
        $any_data['center_page']=$this->model_name->function3();
        $data['content']=$load->view('some_view3',$any_data);
        $this->load->view('main_view',$data);
      }
    
    }
    // some_view:
    <div id="leftside"><?=load_partial('left_side')?></div>
    <div id="centerpage"><?=$center_page?></div>
    <div id="rightside"><?=load_partial('right_side')?></div>
    // load_partial() helper:
    function load_partial($filename) {
        $ci=&get;_instance();
        // load our partials from its own folder:
        $partial=$ci->load->file(APPPATH.'partials/'.$filename,TRUE);
        return $partial;
    }
    // left_side partial:
    $ci=&get;_instance();
    $ci->load->model('some_model','model_name');
    $any_data['partial_content']=$ci->model_name->function();
    $ci->load->view('partial_view',$any_data);
    // right_side partial:
    $ci=&get;_instance();
    $ci->load->model('some_model2','model_name2');
    $any_data['partial_content2']=$ci->model_name2->function();
    $ci->load->view('partial_view2',$any_data);

    I think it works. If anybody want more details, I am glad to explain.

  • #68 / Feb 18, 2008 11:13pm

    wiredesignz

    2882 posts

    Check out this thread Edemilson, you may be interested. I have modules working in a HMVC fashion without core changes to CI.
    http://ellislab.com/forums/viewthread/71940/

  • #69 / Feb 18, 2008 11:19pm

    Edemilson Lima

    241 posts

    I will take a look tomorrow, ok? Right now is too late here… ZZZZzzzzz…
    Thank you!

  • #70 / Feb 19, 2008 7:21am

    Sam Dark

    242 posts

    Here is mine simple approach to partials. Works OK in production for now.

    module.php

    <?php
    class Module {
        private $vars = array();    
        
        function set($name, $value){
            $this->vars[$name] = $value;
        }
        
        function render($template){
            /* @var CI CI_Base */        
            $CI = &get;_instance();
            $CI->load->view($template, $this->vars);
        }
    }
    
    function partial($name, $options = array()){    
        $path = APPPATH.'modules/'.strtolower($name).EXT;
        if(!file_exists($path)) {
            return 'Can\'t find module!';
        }
        require_once $path;    
        $className = $name.'_module';
        $module = new $className();
        
        return $module->process($options);
    }
    ?>

    modules/tags.php

    <?php
    class tags_module extends Module {
        function process($options){        
            /* @var CI CI_Base */        
            $CI = &get;_instance();
        
            $CI->load->model('Post');
            $tags = $CI->Post->getTagList();
            $this->set('tags', $tags);
            $this->render('partials/tags');
        }
    }
    ?>
  • #71 / Feb 19, 2008 12:30pm

    Edemilson Lima

    241 posts

    Check out this thread Edemilson, you may be interested. I have modules working in a HMVC fashion without core changes to CI.
    http://ellislab.com/forums/viewthread/71940/

    Hey man! You was hidding the gold, wasn’t you? 😊

  • #72 / May 12, 2009 12:00pm

    jesims

    1 posts

    this is an interesting post. thank you for sharing 😊
    simulation rachat de credit

  • #73 / May 21, 2009 1:20pm

    katherine04

    1 posts

    😉

  • #74 / May 30, 2009 7:20am

    already710

    1 posts

    Wow! That’s great work. Your posts are very helpful and valuable. Thanks a lot.
    simulation credit

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

ExpressionEngine News!

#eecms, #events, #releases