[eluser]Unknown[/eluser]
Hi there,
Thank you for your tip.
I've found the solution for keeping both controllers working inside controllers sub folders and modular. Although it isn’t fully tested but I thought of posting my solution in here.
I have introduced modular_active function in order to verify the root path
Code:
function modular_active() {
if (isset ( $this->uri->segments [0] )) {
@include (APPPATH . 'config/routes' . EXT);
$this->routes = (! isset ( $route ) or ! is_array ( $route )) ? array () : $route;
$this->default_controller = (! isset ( $this->routes ['default_controller'] ) or $this->routes ['default_controller'] == '') ? FALSE : strtolower ( $this->routes ['default_controller'] );
$uri = $this->uri->segments [0];
if (array_key_exists ( $uri, $this->routes )) {
if (file_exists ( APPPATH . 'controllers/' . $uri . '/' . $this->default_controller . EXT )) {
return FALSE;
}
} else {
return TRUE;
}
}
}
And have modified the existing _validate_request to this
Code:
function _validate_request($segments) {
if ($this->modular_active () == FALSE) {
return parent::_validate_request ( $segments );
} else {
(! isset ( $segments [1] )) and $segments [1] = 'index';
/* locate the module controller */
list ( $path, $file, $home ) = $this->find ( $segments [1], $segments [0] );
if ($path === FALSE)
list ( $path, $file ) = $this->find ( $segments [0], $segments [0] );
/* no controllers were found */
if ($path === FALSE)
show_404 ( $file );
// set the directory path
$this->set_directory ( str_replace ( APPPATH, '', '../' . $path ) );
/* remove the directory segment */
($segments [1] == $file) and $segments = array_slice ( $segments, 1 );
/* set the module home */
($home) and router::path ( $home ) or router::path ( $file );
return $segments;
}
}
Also have removed the overriding fetch_directory function from MY_Router.php
Code:
function fetch_directory()
{
return ($this->directory) ? '../'.$this->directory : '';
}
Now all of the controllers in controllers sub folders and modular are working together in peace
Modified MY_Router.php
Code:
/* define the modules base path */
define ( 'MODBASE', APPPATH . 'modules/' );
class MY_Router extends CI_Router {
function modular_active() {
if (isset ( $this->uri->segments [0] )) {
@include (APPPATH . 'config/routes' . EXT);
$this->routes = (! isset ( $route ) or ! is_array ( $route )) ? array () : $route;
$this->default_controller = (! isset ( $this->routes ['default_controller'] ) or $this->routes ['default_controller'] == '') ? FALSE : strtolower ( $this->routes ['default_controller'] );
$uri = $this->uri->segments [0];
if (array_key_exists ( $uri, $this->routes )) {
if (file_exists ( APPPATH . 'controllers/' . $uri . '/' . $this->default_controller . EXT )) {
return FALSE;
}
} else {
return TRUE;
}
}
}
function _validate_request($segments) {
if ($this->modular_active () == FALSE) {
return parent::_validate_request ( $segments );
} else {
(! isset ( $segments [1] )) and $segments [1] = 'index';
/* locate the module controller */
list ( $path, $file, $home ) = $this->find ( $segments [1], $segments [0] );
if ($path === FALSE)
list ( $path, $file ) = $this->find ( $segments [0], $segments [0] );
/* no controllers were found */
if ($path === FALSE)
show_404 ( $file );
// set the directory path
$this->set_directory ( str_replace ( APPPATH, '', '../' . $path ) );
/* remove the directory segment */
($segments [1] == $file) and $segments = array_slice ( $segments, 1 );
/* set the module home */
($home) and router::path ( $home ) or router::path ( $file );
return $segments;
}
}
/** Locate the module controller **/
function find($file, $path = '', $base = 'controllers/') {
if (($pos = strrpos ( $file, '/' )) !== FALSE) {
$path = substr ( $file, 0, $pos );
$file = substr ( $file, $pos + 1 );
}
$path .= '/';
$paths2scan = array (APPPATH . $base, MODBASE . $path . $base );
foreach ( $paths2scan as $path2 ) {
foreach ( array ($file, ucfirst ( $file ) ) as $name ) {
if (is_file ( $path2 . $name . EXT ))
return array (substr ( $path2, 0, - 1 ), $name, substr ( $path, 0, - 1 ) );
}
}
/* file not found */
return array (FALSE, $file, FALSE );
}
}
class Router {
/** Holds the module name **/
function path($path = NULL) {
static $_path;
($path) and $_path = $path;
return $_path;
}
}