CodeIgniter Forums
default controller in sub dir doesnt work - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: default controller in sub dir doesnt work (/showthread.php?tid=1700)



default controller in sub dir doesnt work - El Forum - 06-21-2007

[eluser]the real rlee[/eluser]
Hi guys not sure this is related to this bug post about sub applications, but i\'ve found i keep getting 404\'s when i set the default_controller to a controller within a subdir. A few echo statements reveals that the class name for the default controller is not getting set properly in Routes.php - it seems to be using the controller path and not the controller name!

Router.php (line 109):
Code:
$this->set_class($this->default_controller);
// is actually setting the class to subdir/controller

And then CodeIgniter.php creates the 404 (line 158):
Code:
$class  = $RTR->fetch_class(); // is subdir/controller
$method = $RTR->fetch_method();
// does a class called \"subdir/controller\" exists - of course not!
if ( ! class_exists($class)
   OR $method == \'controller\'
   OR substr($method, 0, 1) == \'_\'
   OR in_array($method, get_class_methods(\'Controller\'), TRUE)
   )
{



"default controller in sub dir doesnt work" - El Forum - 06-21-2007

[eluser]the real rlee[/eluser]
Further nvesttigation seems to indicate that the CodeIgniter class calls the fetch_directory() function of the Router class, which returns the controller directory ($this->directory) which has only been initialised and not set to the specific controller path! Only later does it get set properly...


"default controller in sub dir doesnt work" - El Forum - 06-21-2007

[eluser]the real rlee[/eluser]
UPDATE: Seems my fix (below) doesnt quiet work when some controllers are in the controllers root dir

For the time being i\'ve added these lines of code to the Router constructor (line 92 just under where default controller is set):

Please note i havent fully tested this but seems to work for me!


Code:
// RL default_controller in subdir patch
        if (strrpos($this->default_controller, \'/\')) {
            $this->directory = substr( $this->default_controller, 0, strrpos($this->default_controller, \'/\')+1);
            $this->default_controller = substr($this->default_controller, strrpos($this->default_controller, \'/\')+1, strlen($this->default_controller));
            
        }



"default controller in sub dir doesnt work" - El Forum - 06-21-2007

[eluser]the real rlee[/eluser]
Hmmm im still testing but this is the last bit of code ive used and seems to be ok now (cut and paste same place as above):

Code:
// RL default_controller in subdir patch
            if (strrpos($this->default_controller, \'/\')) {
                $this->set_directory(substr( $this->default_controller, 0, strrpos($this->default_controller, \'/\')));
                $this->default_controller = substr($this->default_controller, strrpos($this->default_controller, \'/\')+1, strlen($this->default_controller));
            }



"default controller in sub dir doesnt work" - El Forum - 06-21-2007

[eluser]Unknown[/eluser]
it seems like ur solving ur own problem..
keep up the good work!!
i cant answer ur question coz im new here..
hee hee..
God Bless!


"default controller in sub dir doesnt work" - El Forum - 06-21-2007

[eluser]the real rlee[/eluser]
Haha yeah \"thinking out loud\" i call it Tongue. Welcome horns Smile


"default controller in sub dir doesnt work" - El Forum - 06-24-2007

[eluser]marcoss[/eluser]
Let me see if i got your point, you want to do this?

Code:
$route[\'default_controller\']    = \'dir/controller\';

or this,

Code:
$route[\'default_controller\']    = \'dir/controller/some_method\';



"default controller in sub dir doesnt work" - El Forum - 06-24-2007

[eluser]the real rlee[/eluser]
Hi marcoss,

my default_controller is within a subdir like this;

Code:
$route[\'default_controller\']    = \'dir/controller\';

and unfortunately setting the config value doesnt work as it should


"default controller in sub dir doesnt work" - El Forum - 06-24-2007

[eluser]marcoss[/eluser]
You are right, it doesn\'t work as expected :S

You can try this workaround to avoid messing with the code,

Code:
//routes config
$route[\'default_controller\'] = \"dummy\";

//dummy controller
class Dummy extends Controller {

    function Dummy(){
        parent::Controller();
        $this->load->helper(\'url\');
    }
    
    function index(){
        redirect(\'dir/controller\',\'refresh\');
    }
    
}



"default controller in sub dir doesnt work" - El Forum - 01-18-2008

[eluser]sexy22[/eluser]
haha .....Thanks。。I have solved the problem .marcoss reply extremely good.