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?

9
  • Could you show us the contents of /modules/gallery/config/routes.php? Commented Sep 16, 2012 at 11:26
  • $route['gallery/list'] = 'gallery/images/list_items'; $route['gallery/list/(:any)'] = 'gallery/images/list_items/$1'; Commented Sep 16, 2012 at 11:27
  • Have you tried to debug it with the extension I wrote earlier? stackoverflow.com/questions/12446184/… Commented Sep 16, 2012 at 12:05
  • Yeah, the routes are not being hit Commented Sep 16, 2012 at 12:32
  • If you empty the file modules/gallery/config/routes.php. And visit the module 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. Commented Sep 16, 2012 at 14:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.