CodeIgniter Forums

Full Version: default_controller routing with varibles broken
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The issue i've got is the $route['default_controller'] is erroring out when including a controller/method/variable.

It works fine when there is only a controller/method defined as the default_controller, but it doesn't work as documented or as it did in CI2 (latest stable) which I am porting my code over from.

After some initial research and debugging of the system/core/Router.php class shows that the constructor calls a number of functions which decorate and clean the route, but when it hits the _set_default_controller() method after this process the output from the stored class parameter is:

Assumed: class/method

When using a route like this:

$route['default_controller'] = "home/index/1";

The output of the $this->default_controller is then:


This is unexpected and a rather odd way to restructure the segments, I have tried to resolve this, but I don't know enough about the Routing flow, from looking through the other methods it doens't look like CI3 stores the variables for URL's within the segment array.

This method looks like it has been drastically re-written since CI2 and now doesn't work as per CI v2 or the documentation for the routing class. I have been an avid user of CI for years now and haven't seen any major issues like this, the crux of my application relies on this due to caching routes and auto-generating a file which contain cached routes to eliminate hitting the database everytime to validate a dynamic route and serve the page. Due to this re-write I'm un-able to to use the functionality as expected and any route should allow controller/method/variable be it set or dynamic.

Any help would be appreciated as I'm nearly finished on a job upgrading a previous system (with a client pressing for a release) and un-able to finish this due to the functionality here not behaving as expected.

Many thanks
The default controller is only called when going to the "homepage" of your site, IE You shouldn't need to pass parameters to the controller/method for the homepage. Just do that in the home/index controller/method.
(06-12-2015, 12:18 PM)CroNiX Wrote: [ -> ]The default controller is only called when going to the "homepage" of your site, IE You shouldn't need to pass parameters to the controller/method for the homepage. Just do that in the home/index controller/method.

Thanks for your reply, as stated previously in my post I'm generating routes and caching them in a file. This is beside the point, the functionality has been changed from that of CI2 and also away from the current documentation for routing meaning that you cannot use the same notation to format route requests as normal non default_controller routes.

If you've got a big site manually pulling out each route in a database call for routing isn't ideal, nor is it scalable. The architecture i've chosen is to cache routes into a custom generated "cache/routes.php" file and if the slug is equal to "index" which is my home / default page then it uses this as the default route.

I do not want to be repeating code for one instance due to the fact that the functionality has been changed away from the docs and it doesn't work. We could sit here all day working out hacks to bypass bugs in CI i've been doing it for years now, but this is core functionality which shouldn't be messed with.
The documentation states that the default_controller route can be set to a controller name which will be called if the URI contains no data.

The upgrade notes point out that it will also accept a controller/method pair, but that a previous "bug" which allowed you to set a directory/controller, directory/controller/method, or controller/method/value has been "fixed", so this is no longer possible. However, 'default_controller' and '404_override' are now applied per directory, so the default controller can be added to each directory in your controllers directory to handle URLs pointing to a directory without a controller.

In other words, I could not find anything in the documentation which states that a 3-segment value will be accepted for the 'default_controller' route, so the fact that it's being interpreted in a manner which is not desirable is only a bug in the sense that it should probably fail more dramatically.