Welcome Guest, Not a member yet? Register   Sign In
Modular Extensions - HMVC Routing problem
#1

[eluser]Unknown[/eluser]
Hello,

I have some odd issue with HMVC and routes.php in config folder. before having the Modular Extensions files placed into the Library and helper folders everything was working perfectly! but after that i'm just getting 404 error! also by ignoring the show_404($file); line in My_Router.php this message is appearing:


Code:
Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.


What I'm trying to do is simply introducing the custom routing to my administrator controller and its folder which is working without having HMVC My_router.php file in my library folder.

This is what i have in routes.php for re-routing to the administrator controller located inside the administrator folder in controller :


system -> application -> controllers -> administrator -> main.php
Code:
$route['administrator'] = "administrator";

and in administrator controller


Code:
class Main extends Controller {

    function __construct()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $this->load->view('welcome_message');
    }
}

I am guessing that somehow My_router.php is overriding the routes.php in config folder and doesn't allow Router.php to load the defined address's.

Just wondering if you guys have any idea abut how to solve this issue? Many Thanks.

My CI version is: 1.6 and the HMVC version is: 4.2.06
#2

[eluser]wiredesignz[/eluser]
Welcome to CI forums.

By design ME does not work with controller sub-directories, it's better to move your administrator files to a module.

Sub-directories are another form of organizational structure, and with ME module directories have first priority in your application.
#3

[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 Smile


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;
    }
}
#4

[eluser]wiredesignz[/eluser]
Wow, thats a lot of work man. I will certainly check this out. Thanks.
#5

[eluser]Tony Nash[/eluser]
@wiredesignz did you have time to implement above code with ME?.. while trying to use ME + FreakAuth, I noticed subdirectories inside modules very useful but not working with ME.
#6

[eluser]wiredesignz[/eluser]
Hi Tony, Sadly ME 4.2.06 is final, and will not be getting any new features.

You are of course welcome to add this to ME yourself. Wink
#7

[eluser]Tony Nash[/eluser]
Oh dear! are you working with new concept? I mean whole new type of ME code?
#8

[eluser]wiredesignz[/eluser]
Yes, ME 5.1 is in development PHP5 only, but it has no access to controller subdirectories either.

You might consider that freak-auth uses subdirectories because native CI has no modules, after all they are both merely organizational features.




Theme © iAndrew 2016 - Forum software by © MyBB