Welcome Guest, Not a member yet? Register   Sign In
Site Migrate - Easily bring entire websites into a CI system with minimal changes to existing code
#1

[eluser]sophistry[/eluser]
Recently, I've moved several existing sites into the loving embrace of CodeIgniter. Here is a way to copy a whole website full of HTML and PHP pages into the system/application/views directory and have it working in a CI context within minutes. All the paths remain intact so even though CI is serving the pages, the URIs are all the same as the old site.

This technique supports:
- URIs are the same between old site and new CI-context site (may need some custom routing in routes.php)
- easy drop-in of existing websites with very few changes (minor path changes for image, css, and js assets may be necessary)
- nested directories in the views directory
- code is 'directory-aware' with default view file name 'index'
- code is 'current working directory aware' so relative links in include and require functions work properly
- old-school GET queries (you know, with the question mark in the URI... remember those? you used to use them before you started with CI - needs just three config settings changes in config.php to tell CI to allow GET queries into CI)
- adding a method in the Child Controller overrides the automatic view file loading (to move the site gradually to MVC setup)

Here is the solution: Site Migrate

It is one Base Controller (just a few lines long) that you extend with your own child controllers. The wiki page has the full code and a demo child controller for your convenience, but here is what the Base Controller looks like with no comments:
Code:
<?php
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 (method_exists($this,$view_or_method))
        {
            $this->$view_or_method();
        }
        else
        {
            $local_view_path = rtrim($this->uri->ruri_string(),'/');
            $view_file = strtok($local_view_path, '?');
            $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;
            chdir(dirname($server_path_to_view.$view_file));
            $this->load->view($view_file);
            chdir(dirname(FCPATH));
        }
    }
}

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

Quote:



Questions? Suggestions? Brickbats? ;-) p.s. _remap() is cool - thanks to whoever put it in CI.




Theme © iAndrew 2016 - Forum software by © MyBB