Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 23 revisions

a parent/child controller combination to help you migrate html/php sites. supports nested views.

Base controller - the parent class [code]<?php // DESCRIPTION: // use _remap() method to call up view files // via uri if they exist in a subdir of 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 // or to the child classes

// you can drop an entire existing website in views/site_migrate // and the files will be served through the CI view mechanism

class Site_migrate_base extends Controller {

var $default_view_and_method = 'index';

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

function _remap()
{
    $view_or_method = $this->uri->rsegment(2,$this->default_view_and_method);
    
    // if the method exists in any parent/child class, run it
    // else load the view file &#40;even nested views by uri string&#41;
    if (method_exists($this,$view_or_method))
    {
        $this->$view_or_method();
    }
    else
    {
        $local_view_path = rtrim($this->uri->ruri_string(),'/');
        $view_file = (is_dir(APPPATH.'views/'.$local_view_path)) ? $local_view_path.'/'.$this->default_view_and_method : $local_view_path;
        $this->load->view($view_file);
    }
}

// this is a demo function
function test_method_in_parent($in)
{
    echo $in . ' uri sent into class: ' . __CLASS__;
}

}

/* End of file site_migrate_base.php / / Location: ./system/application/controllers/site_migrate_base.php */[/code]

extended controller - the child class

[code]<?php

require_once('Site_migrate_base.php');

class Site_migrate extends Site_migrate_base {

function Site_migrate()
{
    parent::Site_migrate_base();
}

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

}

/* End of file site_migrate.php / / Location: ./system/application/controllers/site_migrate.php */[/code]

Clone this wiki locally