CodeIgniter Forums
HMVC problem: routes.php within module config dir not working - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: HMVC problem: routes.php within module config dir not working (/thread-41851.html)



HMVC problem: routes.php within module config dir not working - El Forum - 05-18-2011

[eluser]Mr. Pickle[/eluser]
I have a weird problem when using HMVC.
I have a module called news. I use a multilingual site, so I have the following in application/config/routes.php:
Code:
$route['es/news'] = 'news/index'; // Make sure the spanish news also uses this module

This works perfect, it loads the news module.
Now I want to re-route certain news url's and I want to do this within the module application/modules/news/config/routes.php:
Code:
$route['(es|en)/(:num)/(:any)'] = 'news/detail/$2';

The problem is the second re-route is not performed. If I copy the exact same line to the main routes.php it works without a problem. I wanted to be sure the routes.php within a module would be loaded so I did a echo with dummy text. This text appears on my page so the routes.php within the module directory IS being loaded.


HMVC problem: routes.php within module config dir not working - El Forum - 05-19-2011

[eluser]toopay[/eluser]
There's no way your routes.php on your modules would works, unless you extending Router.php class (core) and include that route file into _set_routing() function. Because CI router only use routes.php in application/config


HMVC problem: routes.php within module config dir not working - El Forum - 05-19-2011

[eluser]InsiteFX[/eluser]
You can create a directory in your modules directory named config and add your routes.php there.
Code:
modules
  module_name
  -- config
  ---- routes.php
  -- controllers
  -- views

InsiteFX


HMVC problem: routes.php within module config dir not working - El Forum - 05-19-2011

[eluser]toopay[/eluser]
Hahaha, no way that will works.


HMVC problem: routes.php within module config dir not working - El Forum - 05-19-2011

[eluser]wiredesignz[/eluser]
Modular Extensions HMVC uses module level routing "{module}/config/routes.php" but it only works with the result of your application level routing (if any) and does not see the URL directly.


HMVC problem: routes.php within module config dir not working - El Forum - 05-19-2011

[eluser]Mr. Pickle[/eluser]
[quote author="wiredesignz" date="1305807203"]Modular Extensions HMVC uses module level routing "{module}/config/routes.php" but it only works with the result of your application level routing (if any) and does not see the URL directly.[/quote]

@wiredesignz: can you please tell me more about how I have to approach module level routing? I use it together with the i18n library and it seems that this is giving me problems.
The module level routes.php file is loaded, as far as I know the routes exist but CI gives me a 404. When I try the module level routing without using language uri's it works perfect.

I think the problem is that I use the global routes.php (application/config/routes.php) to route url's containing language equivalents of real controllers to that matching controller. For example, to have the Spanish news loading the news module I have:
Code:
$route['/es/noticias/'] = 'news';

Now I want to have a few routings within the module level routes.php because I don't want all module specifics routes in the global routes file (will be a long list and most of it useless for the specific request)
So, my application/modules/news/config/routes/config.php contains
Code:
$route['/es/noticias/(:num)/(:any)'] = "news/detail/$1";

This gives me a 404 if I visit for example:
http://mysite.com/es/noticias/1/test-message

And yes, the function detail does exist within the news controller.
If I move the exact same line from the module level routes.php file to the global routes.php file it works like a charm! Weird innit?


HMVC problem: routes.php within module config dir not working - El Forum - 05-22-2011

[eluser]Mr. Pickle[/eluser]
Nothing people?

If it is not possible to use this solution if you already re-route international names for controllers (so the name in the url is re-routed and does not match any real controller name) from the global routes.php please let me know so I can stop trying and Google-ing.

Might choose to leave it as it is now or have the ugly solution (all possible scenario's for all international routes for all modules in the global routes.php)


HMVC problem: routes.php within module config dir not working - El Forum - 04-05-2012

[eluser]Unknown[/eluser]
@Mr. Pickle

I'm sure you've figured this out by now.

Code:
/* use module route if available */
if (isset($segments[0]) AND $routes = Modules::parse_routes($segments[0], implode('/', $segments))) {
$segments = $routes;
}

As you can see from the excerpt above taken from MX_Router, the code doesn't even consider any of the module's routes unless the first URI segment matches the module name.

So in your application/modules/news/config/routes.php file, any $route whose key does not start with news/ will not be considered.

For instance, the following will work as we expect:
Code:
$route['news/es'] = 'news/index';

But this will not:
Code:
$route['es/news'] = 'news/index';

As you've discovered the es/news example will work in the global routes file application/config/routes.php.

I agree that modules' routes.php files should not have this URI requirement. While modules are meant to organize code, Code Igniter routes is an application-wide feature and a module's routes should be able to rewrite at the root of the URI.

@wiredesignz

What are your thoughts on this? Is there a design reason why it was implemented this way? Perhaps all routes in modular routes file should be considered regardless of the request URI?

Thanks for any response, and great job on MX HMVC.