【发布时间】:2016-01-15 09:32:46
【问题描述】:
虽然在没有子目录的情况下设置$route['default_controller'] 它可以工作,但我想在子目录中更改它并且它不工作。
【问题讨论】:
-
你是怎么去的?你试试我的答案
标签: php codeigniter
虽然在没有子目录的情况下设置$route['default_controller'] 它可以工作,但我想在子目录中更改它并且它不工作。
【问题讨论】:
标签: php codeigniter
默认情况下,codeigniter 3 你将无法在你的 default_route 中有子文件夹
你需要使用 MY_Router.php
位于应用程序> 核心> MY_Router.php
这将使您能够在 CI3 的 default_controller 中使用子文件夹
<?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.');
}
// Is the method being specified?
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
// This is what I added, checks if the class is a directory
if( is_dir(APPPATH.'controllers/'.$class) ) {
// Set the class as the directory
$this->set_directory($class);
// $method is the class
$class = $method;
// Re check for slash if method has been set
if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
}
if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php')) {
// This will trigger 404 later
return;
}
$this->set_class($class);
$this->set_method($method);
// Assign routed segments, index starting from 1
$this->uri->rsegments = array(
1 => $class,
2 => $method
);
log_message('debug', 'No URI present. Default controller set.');
}
}
那么这应该让你能够做到
$route['default_controller'] = 'subfolder/controller/function';
【讨论】:
CodeIgniter Version 3.1.0对我来说不起作用,你在3.1.0 version试过了吗
.htaccess 的错误文件名及其在 CI 3.+ 中的魅力。