codeigniter hmvc routes not working properly codeigniter hmvc routes not working properly codeigniter codeigniter

codeigniter hmvc routes not working properly


I found a way of making the routes from modules working just fine, I don't know if is the ideal solution but works fine so far:

open your application/config/routes.php and underneath $route['404_override'] = ''; add the following code:

$modules_path = APPPATH.'modules/';     $modules = scandir($modules_path);foreach($modules as $module){    if($module === '.' || $module === '..') continue;    if(is_dir($modules_path) . '/' . $module)    {        $routes_path = $modules_path . $module . '/config/routes.php';        if(file_exists($routes_path))        {            require($routes_path);        }        else        {            continue;        }    }}

the following solution works fine even if config folder or routes.php is missing from your module folder


Here's the thing: the module's routes.php only gets loaded when that module is "invoked", otherwise CI would have to load all route configurations from all modules in order to process each request (which does not happen).

You'll have to use your main application's routes.php to get this to work. You aren't using the pages segment in your URL, therefore the routing for that module never gets loaded.

I know that's what you wanted to avoid, but unfortunately it's not possible unless you want to get "hacky".


Here's the routing I use to map requests for admin/module to module/admin, maybe you can use it:

// application/config/routes.php$route['admin']                     = "dashboard/admin"; // dashboard is a module$route['admin/([a-zA-Z_-]+)/(:any)'] = "$1/admin/$2";$route['admin/([a-zA-Z_-]+)']        = "$1/admin/index";$route['(:any)/admin']               = "admin/$1";


You just need this https://gist.github.com/Kristories/5227732.

Copy MY_Router.php into application/core/