我在路由配置default_controller被设置为“home.php”。
我有我的控制器的一个子目录,让我们把它称为“文件夹”。 所以,如果我访问http://mysite.com/folder/ ,默认的控制器“文件夹/ home.php”应该叫吧?
但是出于某种原因,这并不工作,我得到一个404访问http://mysite.com/folder/home或http://mysite.com/folder/home/index按预期工作。 除此之外,默认的控制器工作在根目录( http://mysite.com负载home.php)。
任何想法,任何人都经历吗? 我不能让我的头周围 - 这似乎是一个CI的问题,但我无法找到任何人有同样的问题。
文档,从我的方式理解它至少表明,这应该很好地工作: http://codeigniter.com/user_guide/general/controllers.html#subfolders
设置默认控制器“文件夹/ home.php”是指http://mysite.com/folder/如预期正常工作。 除了我想要的默认控制器只是“home.php” - 无论是在根目录或子目录,在该目录中home.php应该被加载,如文档建议。
干杯
对于在控制器中的每个子文件夹,文件夹必须指定一个默认的控制器routes.php
。 在内置$route['default_controller']
不会为子文件夹的工作。
如:用于设置默认控制器为您folder
的子文件夹中home
以下内容添加到您的/application/config/routes.php
文件:
$route['folder'] = "folder/home";
这意味着http://mysite.com/folder/
相同http://mysite.com/folder/home
作为URL。
您可以扩展系统路由器按照要求,
- 在创建My_Router.php
application/core/
目录
/ * *要改变这种许可证头,选择在项目属性许可头。 *要改变这个模板文件,选择工具| 模板*并打开编辑器中的模板。 * /
/**
* Description of My_Router
*
* @author girish
*/
class My_Router extends CI_Router {
//put your code here
public function __construct($routing = NULL) {
parent::__construct($routing);
}
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', $directory, $class, $method) !== 3) {
$method = 'index';
}
if (is_dir(APPPATH . 'controllers' . DIRECTORY_SEPARATOR . $directory) === true) {
if (!file_exists(APPPATH . 'controllers' . DIRECTORY_SEPARATOR . $directory . DIRECTORY_SEPARATOR . ucfirst($class) . '.php')) {
// This will trigger 404 later
return;
}
$this->set_directory($directory);
$this->set_class($class);
$this->set_method($method);
} else {
if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) {
$method = 'index';
}
if (!file_exists(APPPATH . 'controllers' . DIRECTORY_SEPARATOR . 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.');
}
}
并覆盖_set_default_controller()
从自定义的方法,将来自子目录控制器以及根目录控制器工作。
而在application/config/routes.php
如果你需要的子目录默认控制器,然后
$route['default_controller'] = "admin/admins/login";
- 管理 - 文件夹
- 管理员 - 控制器
- 登录 - 方法
如果你需要根目录默认控制器,然后
$route['default_controller'] = "welcome/index";
不知道它会在所有版本中工作,但在CI3.0.6测试
如果你想保持灵活的,你需要的一切通过启动文件夹(在之后application/config/config.php
):
$route['home'] = "home/whatever";
$route['home/(:any)'] = "home/whatever/$1";
缺省路由是用来告诉CI,其中如果URI不包含任何数据的控制器类应该被加载。
$route['default_controller'] = "unicorn/best";
所以,当我加载
http://example.com/index.php/unicorn/
最好的控制器将被载入。
还当我加载
http://example.com/
要么
http://example.com/index.php/horse/
最好的控制器将被载入。
我的文件夹结构
--controllers
--backend
--frontend
--home.php
--products.php
--productDetail.php
--homeIndex.php
在配置/ routes.php文件
$route['default_controller'] = 'homeIndex';
$route['frontend'] = 'frontend/home';
$route['backend'] = 'backend/home';
在控制器/ homeIndex.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once(APPPATH.'controllers/frontend/Home.php');
class homeIndex extends home {
public function index() {
$this->action();
}
}
默认情况下homeIndex将被加载和homeIndex我打电话到前台的/ home /动作功能。