Welcome Guest, Not a member yet? Register   Sign In
Bug in Default Controller in index.php ?!
#1

[eluser]PhilTem[/eluser]
I'm not sure if it is really a bug or if I just got the user's guide wrong.

Within the index.php under the root-directory of a default CI application there's a section defined by

Code:
/*
* --------------------------------------------------------------------
* DEFAULT CONTROLLER
* --------------------------------------------------------------------
*
* Normally you will set your default controller in the routes.php file.
* You can, however, force a custom routing by hard-coding a
* specific controller class/function here.  For most applications, you
* WILL NOT set your routing here, but it's an option for those
* special instances where you might want to override the standard
* routing in a specific front controller that shares a common CI installation.
*
* IMPORTANT:  If you set the routing here, NO OTHER controller will be
* callable. In essence, this preference limits your application to ONE
* specific controller.  Leave the function name blank if you need
* to call functions dynamically via the URI.
*
* Un-comment the $routing array below to use this feature
*
*/

So I thought, if I set a default controller and function like

Code:
// The directory name, relative to the "controllers" folder.  Leave blank
// if your controller is not in a sub-folder within the "controllers" folder
// $routing['directory'] = '';

// The controller class file name.  Example:  Mycontroller
$routing['controller'] = 'bootstrap';

// The controller function you wish to be called.
$routing['function'] = 'route';

then any url will be routed to 'bootstrap/route' and I can do whatever I want to do.
But somehow, urls like

Code:
example.com/index.php/some_test/text_here

gets actually routed to controller some_test with method text_here.

I was looking at the code of the Router class and came across line 284 where the controller-file is searched. And if it cannot be found, a 404 is shown. But I think that's the problem, since the request gets validated as soon as there are segments in the url and before the overrides are set (line 178 in system/core/CodeIgniter.php).

There is no problem using routes.php and setting

Code:
$route['.+'] = 'bootstrap/route';

but I wanted to use the default-controller setting in index.php since otherwise I'd have many many routes to set up and for me it's easier to use a default bootstrap controller that will route my requests.

Anybody else can confirm this bug? Or confirm my misunderstanding of the documentation?

Thanks a lot in advance Wink
#2

[eluser]jjDeveloper[/eluser]
Have a look at application/config/routes.php

It might help you re-think your strategy.
#3

[eluser]graf[/eluser]
This is a bug, here's how to fix it:
core/Codeigniter.php - line 172
Code:
$RTR =& load_class('Router', 'core');

// Set any routing overrides that may exist in the main index file
if (isset($routing))
{
    $RTR->_set_overrides($routing);
}

$RTR->_set_routing();


And
core/Router.php - line 227 (_set_request function)
Code:
if(!empty($this->directory)){
    array_unshift($segments, str_replace('/','',$this->directory));
}


Hopefully this will get patched in the next release.
#4

[eluser]graf[/eluser]
Actually I did another solution that checks if $routing['diectory'] is set, if so try to serve the controller from that subdirectory, if it does not revert back to the parent controller directory. This was so i could have 1 controller that should exist across all subsites without having to duplicate the controller

/core/Router.php _validate_request() function
added this at line 272, right after the if(count($segments) == 0 ) block

Code:
if(!empty($this->directory)){
    // Check if we should serve from a defined directory
    if (file_exists(APPPATH.'controllers/'.$this->directory.$segments[0].'.php')){
        return $segments;
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB