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]
  • #16 / Feb 20, 2008 10:22am

    wiredesignz

    2882 posts

    Added. Thanks Sam, hope it’s of use. 😉

  • #17 / Feb 20, 2008 10:50am

    Edemilson Lima

    241 posts

    I wouldn’t load a module inside a view, and I wouldn’t use a helper, I would add a function to the class as I suggested. Do the whole thing with one line of code in the view.

    Sorry, I don’t get it… For example, how can I load a module into a <div></div> in my view, with just one line of code, and without loading the module directly from the view? I would like to keep the views and controllers decoupled in respect to the modules. The controller load the view, the view load the modules. If I move a module from one view to another, I don’t want to everytime reflect these changes in the controllers. In this aspect, the modules must be independent from the controllers.

  • #18 / Feb 20, 2008 11:00am

    wiredesignz

    2882 posts

    Edmilson, I have added a new method to load and render any module from your view. You don’t need to load it in a controller unless you need to access it somehow.

    <?php $this->modules->render('any_module'); ?>    //your module must contain a render() method.
  • #19 / Feb 20, 2008 12:58pm

    Kemik

    162 posts

    Is the url still the same?

    E.g. http://www.site.com/module/controller/function ?

    Is there anyway of making it http://www.site.com/controller/function like normally?

  • #20 / Feb 20, 2008 1:04pm

    Edemilson Lima

    241 posts

    Edmilson, I have added a new method to load and render any module from your view.

    In this case, isn’t the index() function of a controller equivalent to the render() function of a module? What happen if I write an index() function in my module? Will it not be executed by default? Instead of a render() method, I could use index() and/or a constructor, to keep it more close to the controller standard. What do you think?

  • #21 / Feb 20, 2008 2:51pm

    wiredesignz

    2882 posts

    Is the url still the same?

    E.g. http://www.site.com/module/controller/function ?

    Is there anyway of making it http://www.site.com/controller/function like normally?

    Sorry, this modules is not the same as Zach’s Matchbox modules.
    This is a HMVC style system where the modules act as psuedo-controllers and can be used to perform a specific MVC process (modular).
    They aren’t aware of the routing or uri structure of an application unless you make it so. I have used the _remap() function in my main controller to decide which module to load and run.

    @Edmilson: Yes you could use index() as the default method in a module.

  • #22 / Feb 20, 2008 4:11pm

    wiredesignz

    2882 posts

    Updates:
    Added run()  - to load and execute a module in one call. Thanks Sam Dark.
    Added index() - as the default run method. Thanks Edmilson Lima.
    Added in_subdir() - as sub-directory path helper

  • #23 / Feb 20, 2008 4:57pm

    Edemilson Lima

    241 posts

    Superb, wonderful! Finally we have a decent HMVC implementation in CI!
    May it need some adjustments, but I think it is following the right path.
    It is simple, easy and flexible, as things must be in our loved framework.
    Now we need more usage examples, a section in the Wiki, etc.
    May it could also goes someday to the core, who knows? 😊

  • #24 / Feb 20, 2008 6:00pm

    Kemik

    162 posts

    So it’s basically groups of controllers, models and views?

    That sounds great, especially with not having to add extra segments to the url! How does it deal with clashes of controllers though?

    E.g. if a news module has a controller called admin (module/controller/function -> news/admin/index) and another module has the same named controller (user/admin/index)

  • #25 / Feb 20, 2008 6:05pm

    wiredesignz

    2882 posts

    You’ll get an error if you try to load two classes with the same name. Blame it on PHP 😛

    It’s only modules as HMVC controllers, Models and Views are still located as per normal.

    I usually create a sub-directory in application/views to match the module name, same with Models if necessary.

    Remember this library will work with Zach’s Matchbox modular separation. It doesn’t replace it. 😉

  • #26 / Feb 21, 2008 4:17am

    Gjoko Pargo

    19 posts

    Hallo everyone,
    My first message here on the forum.
    I used to have my own framework made with PEAR libraries some several years ago, but since then PHP has come a long way, so the framework become quite outdated.

    Since then I’ve started to look for a replacement framework, one which will be able to form cute URL’s, be small and be fast. That’s why I believe I’ve chosen CI. But… before I make my first project I decided to read everything I could find on the subject, including the book “CI for rapid PHP app. devel.”. And then I realized that I don’t have code reuse in my apps, something that standard PHP, PEAR and Savant2/3 used to give me. The ability to define a - I call them - modules (menu, pool box, banners, simple search form), which I will be able to include anywhere in the application and they would work out of the box.

    The style I’m used to work is the following:
    - one view template for header
    - many view templates for the main section (reports, registration forms go here, it’s the central part of the page)
    - one view template for the footer section

    Header then includes the “modules” I’ve mentioned before, such as menu and maybe a search box, while the footer includes “modules” such as banners section or some similar.
    Of course the “main” section is where the main content should be presented to the visitor - texts, pictures, registration forms, etc.

    So I’ve decided to do the forum search. From what I can see, this topic is exactly what I need (please correct me if I’m wrong). But… I don’t find my self able to understand the code logic here.
    I would politely ask you to well, give me some clues, a mini tutorial sort of and tell me and probably other people that wonder - how this code can be used? Where should it be placed, how should the code structure be divided. I believe that one simple example with one “included module/library/view” would be just about enough.

    And please don’t mind my ignorance in MVC, since I do come from modular style of PHP programming.
    Thanks a lot.

    Gjoko Pargo.

  • #27 / Feb 21, 2008 5:01am

    wiredesignz

    2882 posts

    I am currently working on updating a car dealer website to use CI.

    On this site we have a search form that needs managing, so I have created a module to process the form submission, populate the dropdowns from a database and render the form as a view partial. (see image)

    Search module:

    class Search extends HMVC 
    {
        var $_make, $_price, $make_opts, $found;
        
        function Search()
        {
            parent::HMVC();
            $this->load->model('vehicles_model', 'vehicles');
                    
            //get the distinct vehicle makes from the table
            $this->make_opts = $this->vehicles->findAll_makes();    
        }
        
        function index($page = '')    //$page name is provided from the main view
        {
            if ($page == 'showroom') $this->run();
            return $this->render();
        }
        
        function run()
        {
            if ($_POST) // search data from the form
            {
                $_make  = $this->input->post('select-make');
                $_price = $this->input->post('select-price');
            }
            else       //  search data in the url
            {
                $_make  = (int)$this->uri->segment(2);
                $_price = (int)$this->uri->segment(3);        
            }
            
            if ($_make OR $_price)   //use a bit of logic to build database querys
            {
                $this->_make  = $_make;
                $this->_price = $_price;
                
                $search_opts = array(
                    'isonline',
                    'sellprice = 0',
                    'sellprice > 0 AND sellprice < 5000',
                    'sellprice > 4999',
                );
                
                $_qry   = ($_make) ? "make = '{$this->make_opts[$_make]}' AND " : '';
                $_order = ($_price) ? " ORDER BY sellprice" : " ORDER BY make";
                
                $this->found = $this->vehicles->findPaged("{$_qry}{$search_opts[$_price]}{$_order}", 12);
            }
            else 
                $this->found = $this->vehicles->findPaged("isonline ORDER BY make", 12);
        }
        
        function render()    //build the searchbox view partial
        {
            $price_opts = array(
                'All price ranges',
                'New arrivals - POA',
                'Cars under $5000',
                'Cars $5000 and over',
            );
            
            $data = array(
                'select_makes'  => form_dropdown('select-make', $this->make_opts, $this->_make),
                'select_prices' => form_dropdown('select-price', $price_opts, $this->_price)
            );
            
            return $this->load->view('search/box', $data, TRUE);
        } 
    }
  • #28 / Feb 21, 2008 5:10am

    Gjoko Pargo

    19 posts

    I am currently working on updating an old car dealer website to use CI.

    On this site we have a search form that needs managing, so I have created a module to process the form submission, populate the dropdowns from a database and render the form as a view partial. (see image)

    Thats how you call them? Partials? Ok. I believe this piece of code is called from all of the site pages. Can you please tell me how do you accomplish this using CI ?
    It would be great if you could upload your code.

    Much appreciated.

  • #29 / Feb 21, 2008 5:16am

    wiredesignz

    2882 posts

    The Searchbox partial view code:

    <div id="search-box">
        <div class="box-inner">
            <h1>CAR SEARCH</h1>
            <?php echo form_open('showroom')?>
                
                   <label>Car Make
    
                        <?php echo $select_makes ?>
                   </label>
               
                
                   <label>Price Range
    
                        <?php echo $select_prices ?>
                   </label>
                
                <input type="submit" value="Find Now!" id="submit-button" />
            </form>
        </div>
    </div>

    The main View template has this code to load and run the search module:

    <div id="left">
        <?php echo $this->modules->run('search', $page) ?>
    </div>

    If you need more info, I’ll try to help. 😉

  • #30 / Feb 21, 2008 5:55am

    Gjoko Pargo

    19 posts

    Well, I think I am ready to give CI a try. Dude, thank you very much for the code and the time you spent in putting together the example above.  :cheese:

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

ExpressionEngine News!

#eecms, #events, #releases