codeigniter hmvc routes not working properly
Asked Answered
B

3

5

I installed HMVC by wiredesignz but the routes from application/modules/xxx/config/routes.php didn't get recognized at all. Here is an example:

In application/modules/pages/config/routes.php I have:

$route['pages/admin/(:any)'] = 'admin/$1';
$route['pages/admin'] = 'admin';

If I type the URL, domain.com/admin/pages/create it is not working, the CI 404 Page not found appears. If I move the routes to application/config/routes.php it works just fine.

How do I make it work without putting all the admin routes in main routes.php?

I searched the web for over 4 hours but found no answers regarding this problem. I already checked if routes.php from modules is loading and is working just fine, but any routes I put inside won't work.

Bosket answered 7/1, 2013 at 17:17 Comment(0)
B
7

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

Bosket answered 14/1, 2013 at 14:5 Comment(1)
nice job, using it without any bug yet! Thanks. =)Araiza
T
1

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";
Tamarisk answered 7/1, 2013 at 17:26 Comment(6)
thank you for your answer, so practically and basically is useless from this point of view, right ?Bosket
Not sure what you mean, but I'll post an example of my routing since I'm doing something similar.Tamarisk
what I meant was that if you want to use the routes as normal CI routes, you have no chance if you don't "hacky it"? only if you're using the module in the segment :) i hope that clears out, and thank you for sharing your code may i could learn something usefullBosket
Yeah I think you got it, the module's routes don't get loaded unless the module is loaded.Tamarisk
I made this part of routes myself and is working only if in the modules the controller is named "admin" if I name it admin_category, or category same problem the routes aren't working form module config :(Bosket
No problem, the code I posted may be useless as I assumed a lot about your application structure, but it looked like it might be a good generic way to route all your "admin" stuff for your modules.Tamarisk
S
0

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

Copy MY_Router.php into application/core/

Selfgovernment answered 19/6, 2013 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.