7

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.

4
  • try to make folder name 'Home' uppercase Commented May 22, 2015 at 12:50
  • @ShaifulIslam Not working, Commented May 22, 2015 at 12:53
  • 2
    In codeigniter 3 $route['default_controller'] = 'welcome'; must be not in sub folders I would think you would have to make custom loader for that or custom router. Commented Oct 28, 2015 at 22:34
  • 1
    Yes, I confirm that even with CI 3.0.3 the default controller can't be on a subfolder. Commented Nov 28, 2015 at 8:57

3 Answers 3

10

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';

Sign up to request clarification or add additional context in comments.

1 Comment

This works only for one subfolder. How can i do this for 2 or more subfolders?
5

You need not to change anything from files inside CODEIGNITER system folder. Codeigniter allows developer to extend their feature. You can create a file named as MY_Router.php.

<?php
class MY_Router extends CI_Router {
    protected function _set_default_controller() {

        if (empty($this->default_controller)) {

            show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.');
        }
        if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
            $method = 'index';
        }
        if( is_dir(APPPATH.'controllers/'.$class) ) {
            $this->set_directory($class);
            $class = $method;
            if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
                $method = 'index';
            }
        }

        if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
            return;
        }
        $this->set_class($class);
        $this->set_method($method);
        $this->uri->rsegments = array(
            1 => $class,
            2 => $method
        );
        log_message('debug', 'No URI present. Default controller set.');
    }
}

Note: Do not change the file name.

2 Comments

where to place this file?
codeigniter/application/core/MY_Router.php @SanaullahAhmad
0

Try This in routes.php

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.