3

I have hmvc setup and working fine,

I have a gallery module.

The gallery module breaks down into three controllers and it's structure is as follows:

/modules/gallery/
/modules/gallery/config/
/modules/gallery/helpers/
/modules/gallery/controllers/
/modules/gallery/controllers/gallery.php
/modules/gallery/controllers/galleries.php
/modules/gallery/controllers/images.php
/modules/gallery/models/
/modules/gallery/models/galleriesmodel.php
/modules/gallery/models/imagesmodel.php
/modules/gallery/views/dashboard.tpl
/modules/gallery/views/galleries/dashboard.tpl
/modules/gallery/views/images/dashboard.tpl

anyway, I have a function inside my images.php controller called list_items

So I want to map the url
http://example.com/gallery/images/list to
http://example.com/gallery/images/list_items

So I thought, sweet as, I will just add a /modules/gallery/config/routes.php with the route inside it.

But it seems the routes are not being included.

The routes from /application/config/routes.php are included and if I put a die('loaded') in the module routes.php the it does kill the script,

But running

print_r($this->router) from one of the controllers does not show up any of the routes from the module routes.php.

What's going on here?

Hailwood
  • 89,623
  • 107
  • 270
  • 423
  • Could you show us the contents of /modules/gallery/config/routes.php? – Philo Sep 16 '12 at 11:26
  • `$route['gallery/list'] = 'gallery/images/list_items';` `$route['gallery/list/(:any)'] = 'gallery/images/list_items/$1';` – Hailwood Sep 16 '12 at 11:27
  • Have you tried to debug it with the extension I wrote earlier? http://stackoverflow.com/questions/12446184/debugging-routes-in-codeigniter/12446419 – Philo Sep 16 '12 at 12:05
  • Yeah, the routes are not being hit – Hailwood Sep 16 '12 at 12:32
  • If you empty the file modules/gallery/config/routes.php. And visit the module http://yoursite.com/gallery you should get the following error: `application/modules/gallery/config/routes.php does not contain a valid route array` This error should indicate that the file gets loaded. If you don't get this error, we know that it does not recognize the routes file. – Philo Sep 16 '12 at 14:01
  • And, to confirm, you haven't updated any other core files? – seangates Sep 18 '12 at 00:49
  • Because I am using hmvc there are the hmvc core overrides e.g. MY_router.php but not on top of that – Hailwood Sep 18 '12 at 05:10
  • Why don't you put a routing system in the controller itself instead ? – David Bélanger Sep 21 '12 at 13:51
  • Because I have several controllers per module. And I don't want to have to remap every function – Hailwood Sep 21 '12 at 23:19

2 Answers2

1

As far as I know,

HMVC only looks for requested controllers inside each module, with different hierarchy models, but route overrides using routes.php within modules are never read.

Look into MX_Router::locate it never looks for routes.php inside any module.

Also it does not override CI_Router::_set_routing which looks for routes.php in config folder.

You will have to override CI_Router::_set_routing and read config/router.php in every available module for your module route overrides to work.

Broncha
  • 3,794
  • 1
  • 24
  • 34
0

Broncha shows the way, I show you the code. I use this SOLUTION:

You have to edit _set_routing() in system/core/Router.php or extend this method (better) within MY_Router class (third_party/Router.php).

Now it should always load module/config/routes.php... (it's around line 140)

// Load the routes.php file.
        if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
        {
            include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
        }
        elseif (is_file(APPPATH.'config/routes.php'))
        {
            include(APPPATH.'config/routes.php');
        }

        // Include routes every modules
        $modules_locations = config_item('modules_locations') ? config_item('modules_locations') : FALSE;
        if(!$modules_locations)
        {
            $modules_locations = APPPATH . 'modules/';
            if(is_dir($modules_locations))
            {
                $modules_locations = array($modules_locations => '../modules/');
            }
            else
            {
                show_error('Modules directory not found');
            }
        }
        foreach ($modules_locations as $key => $value)
        {
            if ($handle = opendir($key))
            {
                while (false !== ($entry = readdir($handle)))
                {
                    if ($entry != "." && $entry != "..")
                    {
                        if(is_dir($key.$entry))
                        {
                            $rfile = Modules::find('routes'.EXT, $entry, 'config/');
                            if($rfile[0])
                            {
                                include($rfile[0].$rfile[1]);
                            }
                        }
                    }
                }
                closedir($handle);
            }
        } 

        $this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
        unset($route);

I hope it will help...

Michaelo
  • 115
  • 1
  • 8