Skip to content

Pages HMVC Module Modular Extensions

wiredesignz edited this page · 5 revisions
Clone this wiki locally

Category:Module | Category:Module::Site Migration | Category:Module::Pages

My First Module Contribution: a simple module that can autoload views - Pages! It is useful to help migrate a old-school, page-based website to a CI/HMVC site.

NOTE: this module can be used "as-is" with no modifications as a normal controller - it does not need to be a module; i just like modules. ;-)

To use, create a new module directory called "pages" and put these files into pages/controllers and pages/views:

the parent controller class

<?php

class Page_base extends Controller {

    function Page_base()
    {
        parent::Controller();    
    }

    function _remap()
    {
        // use _remap() method to call up view files
        // via uri if they exist in the module's views dir
        // but only if there is no method in the controller
        // this allows "overriding" default view files
        // just by adding methods to this class
        $default_view = 'index';
        $view = $this->uri->rsegment(2,$default_view);

        if (method_exists($this,$view))
        {
            $this->$view($this->uri->ruri_string());
            exit();
        }
        else
        {
            $this->load->view($this->uri->rsegment(1).'/'.$view);
        }
    }


    function page_method_in_parent($in)
    {
        echo $in . ' uri sent into class: ' . __CLASS__;
    }
}

/* End of file page_base.php */
/* Location: ./application/modules/pages/controllers/page_base.php */

the child class

<?php

require_once('Page_base.php');

class Pages extends Page_base {

    function Pages()
    {
        parent::Page_base();
        $this->load->helper('url');
    }

    function page_method_in_child($in)
    {
        echo $in . ' uri sent into class: ' . __CLASS__;
    }
}

/* End of file pages.php */
/* Location: ./application/modules/pages/controllers/pages.php */

This is the index.php view file (called by default in the _remap setup)

<?php echo date('Ymd H:i:s'); ?><br />
 this is an INDEX PAGE!<br />
<?php echo anchor('pages/another'); ?>;<br />
<?php echo anchor('pages/page_method_in_child'); ?><br />
<?php echo anchor('pages/page_method_in_parent'); ?>
<?php // Location: ./application/modules/pages/views/index.php ?>

Start at this url:

http://domain.com/pages

or even this url:

http://domain.com/pages/index  

and it loads the index page ( in the index page is the link: http://domain.com/pages/another )

When you click the anchor link to go to "pages/another" the request goes through _remap and determines that there is no method called "another" in the child controller class or the parent controller class and tries to load the uri segment as a view file. But, unless you put a file named another.php there will be a CI file not found error. Perfect!

At this point you can write a method named "another()" in either the child (pages.php) or parent class (page_base) or put a view file called another.php in the modules/pages/views directory or even in the good-old application/views directory.

Now, click the other links in index.php (demonstrating calling controller methods in the parent and child classes) and see what happens.

Feedback?

Something went wrong with that request. Please try again.