default controller inside subfolder codeigniter 3 not working

codex picture codex · May 22, 2015 · Viewed 7.9k times · Source

In codeigniter 3 application i have directory structure like this:

-Myproject
  -application
    -controllers
     -home
       Welcome.php   //This is my controller inside home directory

How to set Welcome controller as default controller? I use below code

$route['default_controller'] = 'home/Welcome';

This routing works for previous versions of codeigniter.

Answer

nurulhudamustaqim picture nurulhudamustaqim · Dec 28, 2015

By default, you are not allowed to do that. To get around this, you need to hack your system Router.php:

codeigniter/system/core/Router.php

Edit a few lines of code so that it becomes like this:

codeigniter/system/router.php

line 1. if (!sscanf($this->default_controller, '%[^/]/%[^/]/%s', $directory, $class, $method) !== 2)

line 2. if ( ! file_exists(APPPATH.'controllers'. DIRECTORY_SEPARATOR . $directory. DIRECTORY_SEPARATOR .ucfirst($class).'.php'))

line 3. $this->set_directory($directory);

Once you've done, you can call the default controller under directory.

$route['default_controller'] = 'home/Welcome';