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

This is a solution for people that are looking for a way to keep associated models, views and controllers in the same directory and thereby making their application more modular.

[h3]Installaion[/h3]

Modular separation uses two extended core classes; MY_Loader and MY_Router, both of which should be placed in your application/libraries/ folder.

[b]application/libraries/MY_Loader.php[/b] [code]<?php if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

class MY_Loader extends CI_Loader { function model($model, $name = '', $db_conn = FALSE) { $module = ''; $CI = &get_instance();

    if (is_dir(APPPATH . 'modules/' . $CI->uri->segment(1))) {
        $module = $CI->uri->segment(1);
    }

    if (is_dir(APPPATH . 'modules/' . $CI->uri->segment(2) . '/controllers/' . $CI->uri->segment(1))) {
        $module = $CI->uri->segment(2);
    }

    if (file_exists(APPPATH . 'modules/' . $module . '/models/' . $model . EXT)) {
        $model = '../modules/' . $module . '/models/' . $model;
    }

    parent::model($model, $name, $db_conn);
}

function view($view, $vars = array(), $return = FALSE)
{
    $module = '';
    $CI = &get_instance();

    if (is_dir(APPPATH . 'modules/' . $CI->uri->segment(1))) {
        $module = $CI->uri->segment(1);
    }

    if (is_dir(APPPATH . 'modules/' . $CI->uri->segment(2) . '/controllers/' . $CI->uri->segment(1))) {
        $module = $CI->uri->segment(2);
    }

    if (file_exists(APPPATH . 'modules/' . $module . '/views/' . $view . EXT)) {
        $view = '../modules/' . $module . '/views/' . $view;
    }

    parent::view($view, $vars, $return);
}

} ?>[/code]

[b]application/libraries/MY_Router.php[/b] [code]<?php if (!defined('BASEPATH')) { exit('No direct script access allowed'); }

class MY_Router extends CI_Router { function _set_route_mapping() { if ($this->config->item('enable_query_strings') === true && isset($_GET[$this->config->item('controller_trigger')])) { $this->set_class($_GET[$this->config->item('controller_trigger')]);

        if (isset($_GET[$this->config->item('function_trigger')])) {
            $this->set_method($_GET[$this->config->item('function_trigger')]);
        }

        return;
    }

    @include(APPPATH . 'config/routes' . EXT);
    $this->routes = (!isset($route) || !is_array($route)) ? array() : $route;
    unset($route);

    $this->default_controller = (!isset($this->routes['default_controller']) || $this->routes['default_controller'] == '') ? false : strtolower($this->routes['default_controller']);

    $this->uri_string = $this->_get_uri_string();

    if ($this->uri_string == '/') {
        $this->uri_string = '';
    }

    if ($this->uri_string == '') {
        if ($this->default_controller === false) {
            show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
        }

        // Is the default_controller an accelerant?
        if (is_dir(APPPATH . 'modules/' . $this->default_controller)) {
            $this->set_directory('../modules/' . $this->default_controller . '/controllers/');
        }

        $this->set_class($this->default_controller);
        $this->set_method('index');

        log_message('debug', "No URI present. Default controller set.");
        return;
    }
    unset($this->routes['default_controller']);

    if ($this->config->item('url_suffix') != "") {
        $this->uri_string = preg_replace("|" . preg_quote($this->config->item('url_suffix')) . "$|", "", $this->uri_string);
    }

    foreach (explode("/", preg_replace("|/*(.+?)/*$|", "\\1", $this->uri_string)) as $val) {
        $val = trim($this->_filter_uri($val));

        if ($val != '') {
            $this->segments[] = $val;
        }
    }

    $this->_parse_routes();

    $this->_reindex_segments();
}

function _validate_segments($segments)
{
    // Is the requested controller an accelerant but not the default accelerant controller?
    if (count($segments) > 1 && file_exists(APPPATH . 'modules/' . $segments[0] . '/controllers/' . $segments[1] . EXT)) {
        $this->set_directory('../modules/' . $segments[0] . '/controllers');
        $segments = array_slice($segments, 1);

        return $segments;
    }

    // Is the requested controller an default accelerant controller?
    if (file_exists(APPPATH . 'modules/' . $segments[0] . '/controllers/' . $segments[0] . EXT)) {
        $this->set_directory('../modules/' . $segments[0] . '/controllers');

        return $segments;
    }

    // Is the requested controller an accelerant but not the default accelerant controller in a sub-folder?
    if (count($segments) > 2 && file_exists(APPPATH . 'modules/' . $segments[1] . '/controllers/' . $segments[0] . '/' . $segments[2] . EXT)) {
        $this->set_directory('../modules/' . $segments[1] . '/controllers/' . $segments[0]);
        $segments = array_slice($segments, 2);

        return $segments;
    }

    // Is the requested controller an default accelerant controller in a sub-folder?
    if (count($segments) > 1 && file_exists(APPPATH . 'modules/' . $segments[1] . '/controllers/' . $segments[0] . '/' . $segments[1] . EXT)) {
        $this->set_directory('../modules/' . $segments[1] . '/controllers/' . $segments[0]);
        $segments = array_slice($segments, 1);

        return $segments;
    }

    parent::_validate_segments($segments);
}

} ?>[/code]

Clone this wiki locally