嗨,我不会做那样的事情。
http://example.com/ - 主控制器
http://example.com/rules/ - 从数据库获取内容的主控制器,但如果不存在则返回 404 页面。(没关系,没问题。)
但是,如果我在 application/controlles/rules/ 中有子文件夹,我想将其重定向到 Rules 文件夹中的 Main Contorller。
下面的代码可以解决问题,但我不知道它是如何正确实现的。在 routes.php:
$route['default_controller'] = "main";
$route['404_override'] = '';
$dirtest = $route['(:any)'];
if (is_dir(APPPATH.'controllers/'.$dirtest)) {
$route['(:any)'] = $dirtest.'/$1';
} else {
$route['(:any)'] = 'main/index/$1';
}
好的,我有什么:
控制器/main.php
class Main extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('main_model');
}
public function index($method = null)
{
if (is_dir(APPPATH.'controllers/'.$method)) {
// Need re-rout to the application/controllers/$method/
} else {
if ($query = $this->main_model->get_content($method)) {
$data['content'] = $query[0]->text;
// it shows at views/main.php
} else {
show_404($method);
}
}
$data['main_content'] = 'main';
$this->load->view('includes/template', $data);
}
}
再次更新(routes.php):所以,看起来就像我搜索的内容(工作示例):
$route['default_controller'] = "main";
$route['404_override'] = '';
$subfolders = glob(APPPATH.'controllers/*', GLOB_ONLYDIR);
foreach ($subfolders as $folder) {
$folder = preg_replace('/application\/controllers\//', '', $folder);
$route[$folder] = $folder.'/main/index/';
$route[$folder.'/(:any)'] = $folder.'/main/$1';
}
$route['(:any)'] = 'main/index/$1';
但是,完全需要这样的一些:
http://example.com/1/2/3/4/5/6/...
- 文件夹“控制器”有子文件夹“1”?
- 是:文件夹“1”有子文件夹“2”?
- 否:文件夹“1”有控制器“2.php”?
- 否:控制器“controllers/1/main.php”具有功能“2”?
- 是:重定向到 http://example.com/1/2/ - 其中 3,4,5 - 参数..
当你有这样的结构时,这真的很好:
http://example.com/blog/ - recent blog's posts
http://example.com/blog/2007/ - recent from 2007 year blog's posts
http://example.com/blog/2007/06/ - same with month number
http://example.com/blog/2007/06/29/ - same with day number
http://example.com/blog/web-design/ - recent blog's post's about web design
http://example.com/blog/web-design/2007/ - blog' posts about web design from 2007 years.
http://example.com/blog/current-post-title/ - current post
我发现同样有趣http://codeigniter.com/forums/viewthread/97024/#490613