-
Notifications
You must be signed in to change notification settings - Fork 26
site migrate
This code started life as an experiment in auto-loading views. I needed to be able to serve up already-existing HTML and PHP pages inside a CI context. This is how I did it with a minimum of fuss.
I wanted to be able to skip writing a controller method for every static page I wanted in my site. So I used _remap() to handle incoming URIs. The controller first checks if there is a controller method anywhere in the child or parent class. If there is not a method, it checks the filesystem for a matching view file. If there is a matching view file, then it loads it through the normal CI load->view() method.
So, it lets you put static pages in the views directory and your controller can load them inside a CI context without re-writing them or specifically coding a controller for each page. But, it also helps you migrate your existing site so that it progressively gets more CI-like.
In the grand scheme, it lets you “drop in” a fully functional static (or dynamic) existing website into the views directory of a new CI install and have it work inside a CI context (controllers, models, and views, etc…). Then, when you are ready to start adding dynamic stuff/overriding the static pages, you just add a child controller and methods in that controller. The methods take precedence over the auto-loading of static view files.
For each view file you want to stop auto-loading, just write a controller method that has the same name - it will take precedence.
This is 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 (even nested views by uri string)
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
$server_path_to_view = APPPATH.'views/';
$view_file = (is_dir($server_path_to_view.$view_file)) ? $view_file.'/'.$this->default_view_and_method : $view_file;
// change the current working directory
// to support relative include/require paths
chdir(dirname($server_path_to_view.$view_file));
$this->load->view($view_file);
chdir(dirname(FCPATH));
}
}
// 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]