Welcome Guest, Not a member yet? Register   Sign In
Extension to routing class
#1

[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
#2

[eluser]darkhouse[/eluser]
I hate to rain on your parade, but modifying the core is generally a bad idea. I was able to extend the router library and get a default controller if controller files were not found. I'm sure that could be furthered to get your module controller stuff working. Here's my solution if you want it.

http://ellislab.com/forums/viewthread/10...15/#532408
#3

[eluser]ntheorist[/eluser]
well, i'm aware of the reluctance to modify core files. However i don't see anything wrong with it if it remains compatible with the rest of the framework yet allows for new functionality.

Because the core is limited to looking only into the application/controllers directory for the controller, it requires a workaround to find controllers in another directory. HMVC does it by prepending $this->directory with '../', and doesn't have to make the core change. I suppose i could do that (which would prolly be the solution you're talking about in your lib) or strip the module support out altogether if not needed and it wouldn't matter.

I guess i preferred to be able to set the base controller folder explicitly and without having to use relative file paths, and have further access to it in other parts of my app (separately from the specified directory). Also i'm able to use $this->set_base() to remap to admin directories before controller loading - or even separate site directories under the same CI installation.

The other core change seems so obivous to me i don't know why it wasn't ever introduced, because it enables much more flexible use of the _remap function.

anyway this is all part of a larger project i'm working on but i hoped this was generalized enough to be helpful to others seeking certain functionality in their own apps.

thx for your comments!

n
#4

[eluser]darkhouse[/eluser]
As long as you don't upgrade, you're right, there's no problem. But if you hand the site off to another developer who's unaware of the core mods, and they upgrade the framework, they'd have quite a mess. It's fine for your own purposes, but I wouldn't recommend anyone modify the core.

There are a number of things that should be available in the core that aren't, one in particular is the ability to have nested sub folders in controllers. The router extension I built my solution from addressed that. For large applications, this is pretty much a necessity.

Anyways good luck with everything.
#5

[eluser]ntheorist[/eluser]
yeah i managed to work in nested subfolders as well, i agree for larger and cms apps single level directories aren't very practical. Doing a search for controller subdirectory in the forum demonstrates the idea's popularity.

I can see how the situation you describe with multiple developers could get sticky with core changes. Though In CI's case they haven't made core upgrades for almost a year, which speaks of its simplicity – it just works for most people.

the next CI upgrade will prolly be a major one, something you might need to install from scratch anyway. Many agree that if it comes it will be on the heels of EE 2 - when that will happen tho exactly remains a mystery but it's picking up steam and trust me, i'd love to be able to ditch core hacks in favor of a new revision with more flexibility.

cheers




Theme © iAndrew 2016 - Forum software by © MyBB