CodeIgniter(3.1.4)框架中设置默认控制器

创建的目录结构:

如果是以上这种目录分布结构,则在controller文件夹下没有相应的控制器文件。如果在浏览器中直接使用 【http://localhost】则找不到相应的控制器。

必须进行以下设置:

第一:修改代码:

/**
 * 3.1.4 原始代码 - [system/Router.php] - _set_default_controller();
 */
// if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))
// {
// 	// This will trigger 404 later
// 	return;
// }

/**
 * 3.1.4 修复代码 - [system/Router.php] - _set_default_controller();
 *
 * 修复 - 不能将默认控制器放在子目录中
 */
if ( ! file_exists(APPPATH . 'controllers/' . $this->directory . ucfirst($class) . '.php'))
{
    $path_arr = explode('/', trim($this->default_controller, '/'));

    $class = ucfirst($path_arr[1]);
    $method = isset($path_arr[2]) ? $path_arr[2]: 'index';

    if (file_exists(APPPATH . 'controllers/' . $this->directory . $path_arr[0]. '/' . $class . '.php'))
    {
        $this->directory .= $path_arr[0]. '/';
    }
}

  

第二:修改application/config/route.php文件中的默认控制器。

 控制器中的方法:

* 可以调用控制器中的任意方法。

最后:进行调用:

 修复后,可以默认调用子文件夹中的控制器。

原文地址:https://www.cnblogs.com/hfultrastrong/p/6786547.html