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


class Page_base extends Controller {

    function Page_base()

    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))

    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



class Pages extends Page_base {

    function Pages()

    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:

or even this url:  

and it loads the index page ( in the index page is the link: )

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.


Something went wrong with that request. Please try again.