[eluser]ntheorist[/eluser]
Here's an class extension i'm playing with to add a few features to the built in CI Routing class. Any help testing it out or optimizing/adding anything would be super helpful. Currently its just a drop-in extend plus a simple core edit (outlined below). Otherwise it should still run any current setups normally. In order for the new functionality to work properly, you MUST have your default_controller set in your config/routes.php file. Also, this is made for php5 but may work in php4 if you remove the public/private declarations.
I wrote this because i wanted to be able to have
modules,
multi-level controller directories and a
method call fallback option. There are many ideas and much need for this on the forums i think. I would still like to add a 404 fallback internally as well, but for right now i'm handling it with a _remap on each default controller. At the moment you need to have at least a default_controller in each controller directory or it triggers show_error()
It validates the URI segments in the following cascading order :
1) app controller files
2) app controller sub-directories
3) module controller files
4) module controller sub-directories
5) default module controller method fallback
6) default app controller method fallback
default controller method fallback occurs when no controller file is found and there are still segments. You can have domain.com/example for instance, and example wouldn't have to be a controller of its own (calling essentially domain.com/example/index), rather it attempts to call 'example' as a
method of the default controller domain.com/{default_controller}/example. It will carry over parameters too, so domain.com/example/1/2/3 would call 1, 2 & 3 as parameters of the example() method in the default controller. This only occurs when a controller file is not found, it will also apply within controller sub-directories. Further missing page errors should be handled by _remap() perhaps. (inclusion is in works)
In file /system/codeigniter/CodeIgniter.php (from current 1.7.1 SVN)
@ Line 149 Change
Code:
if ( ! file_exists(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT))
{
show_error('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.');
}
include(APPPATH.'controllers/'.$RTR->fetch_directory().$RTR->fetch_class().EXT);
TO
Code:
if ( ! file_exists( $RTR->fetch_base() . $RTR->fetch_directory() . $RTR->fetch_class() . EXT))
{
show_error('Unable to load your default controller. Please make sure the controller specified in your Routes.php file is valid.');
}
include( $RTR->fetch_base().$RTR->fetch_directory().$RTR->fetch_class().EXT);
Also, personally i change Line 275 from (not really required but nice)
Code:
$CI->_remap($method);
// To
$CI->_remap($method, array_slice($URI->rsegments, 2));
// So that when i call remap on my home controllers it can pass the segments array
Attached is the file. If you try it out please let me know if you find it useful or have any ideas or run into any problems. Thx!
n