可以扩展路由器类。
在/应用/核心创建一个名为MY_Router.php文件(MY是默认前缀),并拷贝到这一点;
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
function set_class($class) {
$this->class = str_replace('-', '_', $class);
}
function set_method($method) {
$this->method = str_replace('-', '_', $method);
}
function _validate_request($segments) {
// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.str_replace('-', '_', $segments[0]).'.php')) {
return $segments;
}
// Is the controller in a sub-folder?
if (is_dir(APPPATH.'controllers/'.$segments[0])) {
// Set the directory and remove it from the segment array
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
if (count($segments) > 0) {
// Does the requested controller exist in the sub-folder?
if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().str_replace('-', '_', $segments[0]).'.php')) {
show_404($this->fetch_directory().$segments[0]);
}
} else {
$this->set_class($this->default_controller);
$this->set_method('index');
// Does the default controller exist in the sub-folder?
if (! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php')) {
$this->directory = '';
return array();
}
}
return $segments;
}
// Can't find the requested controller...
show_404($segments[0]);
}
}
这会自动重写 - 为了你。
如果您不希望下划线更改代码以将其替换为无;
的str_replace('-', '_',
到str_replace('-', '',
好,进入filepath是**本地主机/ mysite的/ index.php的**,但是我已经在另一对条件规则中处理这个条件:** RewriteCond $ 1!^(index.php | css | img | scripts | ckeditor | robots.txt | sitemap.xml | sitemap.xml.gz) RewriteRule ^(。*)$指数。php/$ 1 [L] ** –
如果我正确注册,'-'是一个正则表达式条件,你用'\ -' –
尝试过我也试着用'^([^ \ - ] +) \ - ([^ \ - ] +)$'和'^([^ \ - ] +) - ([^ \ - ] +)$'问题依然存在。 –