7

在 codeigniter 3 应用程序中,我的目录结构如下:

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

如何将 Welcome 控制器设置为默认控制器?我使用下面的代码

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

此路由适用于以前版本的 codeigniter。

4

3 回答 3

10

默认情况下,您不允许这样做。为了解决这个问题,你需要破解你的系统Router.php

codeigniter/system/core/Router.php

编辑几行代码,使它变成这样:

codeigniter/system/router.php

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

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

第 3 行。$this->set_directory($directory);

完成后,您可以调用目录下的默认控制器。

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

于 2015-12-28T20:21:45.270 回答
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.

于 2018-02-14T11:38:18.350 回答
0

在 routes.php 中试试这个

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
于 2019-05-07T14:18:11.130 回答