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 and (when enabled in CI config.php file) GET queries.

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

// OPTIONAL: support for GET queries // you need to change these config items to // allow GET query strings to reach your files // added =&? to support queries in the URI // $config['permitted_uri_chars'] = '=&?a-z 0-9~%.:_-'; // must enable query strings // $config['enable_query_strings'] = TRUE; // may have to change the uri_protocol (see config.php for options) // $config['uri_protocol'] = 'REQUEST_URI';

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
    {
        // the ruri string has a trailing slash we don't need
        $local_view_path = rtrim($this->uri->ruri_string(),'/');
        // if there is a question mark, pull out the filename for passing to view
        $view_file = strtok($local_view_path, '?');
        // append the default_view string if ruri targets a directory
        $view_file = (is_dir(APPPATH.'views/'.$view_file)) ? $view_file.'/'.$this->default_view_and_method : $view_file;
        $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