CI的URI路由模式大家都清楚,它还支持强大的路由重定向:
/**
* Router
*
* @link http://www.phpddt.com
*/
class CI_Router {
//配置类
var $config;
//路由信息,routes.php中配置的
var $routes = array();
/**
* List of error routes
*
* @var array
* @access public
*/
var $error_routes = array();
//当前控制器名或类名
var $class = '';
//当前控制器方法
var $method = 'index';
//目录名
var $directory = '';
//默认控制器
var $default_controller;
/**
* Constructor
*
* Runs the route mapping function.
*/
function __construct()
{
$this->config =& load_class('Config', 'core');
$this->uri =& load_class('URI', 'core');
log_message('debug', "Router Class Initialized");
}
// --------------------------------------------------------------------
/**
* 设置路由映射
*/
function _set_routing()
{
$segments = array();
//如果URL是enable_query_strings
if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
{
//获取目录名
if (isset($_GET[$this->config->item('directory_trigger')]))
{
$this->set_directory(trim($this->uri->_filter_uri($_GET[$this->config->item('directory_trigger')])));
$segments[] = $this->fetch_directory();
}
//获取控制器名
if (isset($_GET[$this->config->item('controller_trigger')]))
{
$this->set_class(trim($this->uri->_filter_uri($_GET[$this->config->item('controller_trigger')])));
$segments[] = $this->fetch_class();
}
//获取方法名
if (isset($_GET[$this->config->item('function_trigger')]))
{
$this->set_method(trim($this->uri->_filter_uri($_GET[$this->config->item('function_trigger')])));
$segments[] = $this->fetch_method();
}
}
// 加载routes.php,引入路由配置信息
if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/routes.php'))
{
include(APPPATH.'config/'.ENVIRONMENT.'/routes.php');
}
elseif (is_file(APPPATH.'config/routes.php'))
{
include(APPPATH.'config/routes.php');
}
//获取路由配置信息
$this->routes = ( ! isset($route) OR ! is_array($route)) ? array() : $route;
unset($route);
//获取到配置信息得到默认控制器
$this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
// 如果得到$segments,就直接确定路由,结束本函数
if (count($segments) > 0)
{ //_validate_request()执行后就确定路由了
return $this->_validate_request($segments);
}
// 获取并确定uri_string=》将URI.php
$this->uri->_fetch_uri_string();
// 如果uri_string为空,只能使用默认路由了
if ($this->uri->uri_string == '')
{
return $this->_set_default_controller();
}
// CI允许在uri后面加后缀url_suffix,但对路由分析没用,去掉
$this->uri->_remove_url_suffix();
// 用/分隔uri_string,将结果保存到segments中
$this->uri->_explode_segments();
// 解析路由(检查重定向后设置路由)
$this->_parse_routes();
// 这个是重建索引,将数组从下标1开始,这是为了你的使用习惯,如$this->uri->segment(1)就是获取第一段
$this->uri->_reindex_segments();
}
// --------------------------------------------------------------------
/**
* 设置默认路由
*/
function _set_default_controller()
{
//如果没有默认控制器,show error
if ($this->default_controller === FALSE)
{
show_error("Unable to determine what should be displayed. A default route has not been specified in the routing file.");
}
// 哦,原来,routes.php中$route['default_controller'] = "welcome";还可以设置默认方法$route['default_controller'] = "welcome/index";
if (strpos($this->default_controller, '/') !== FALSE)
{
$x = explode('/', $this->default_controller);
$this->set_class($x[0]);
$this->set_method($x[1]);
$this->_set_request($x);
}
else
{
$this->set_class($this->default_controller);
$this->set_method('index');
$this->_set_request(array($this->default_controller, 'index'));
}
// 重建segments索引,以1开始
$this->uri->_reindex_segments();
log_message('debug', "No URI present. Default controller set.");
}
// --------------------------------------------------------------------
/**
* 设置路由
*/
function _set_request($segments = array())
{
//_validate_request()返回的是array(controller,method,xxx,xxx...)
$segments = $this->_validate_request($segments);
if (count($segments) == 0)
{
//_set_default_controller()中也调用了_set_request()
return $this->_set_default_controller();
}
$this->set_class($segments[0]);
if (isset($segments[1]))
{
// A standard method request
$this->set_method($segments[1]);
}
else
{
// 如果没有设置method,则index为默认的method
$segments[1] = 'index';
}
// URI里面的这个URI::$rsegments是经过处理,并确定后的路由,实质调用的路由的段信息
$this->uri->rsegments = $segments;
}
// --------------------------------------------------------------------
/**
* 验证segments数组
*/
function _validate_request($segments)
{
if (count($segments) == 0)
{
return $segments;
}
// 请求的controller是否存在
if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
{
return $segments;
}
// 如果controller文件不存在,那么它是否在子目录下
if (is_dir(APPPATH.'controllers/'.$segments[0]))
{
// 设置目录,并将它从segment数组中剔除
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
if (count($segments) > 0)
{
// 如果当前controller文件不存在,则show 404
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].'.php'))
{
if ( ! empty($this->routes['404_override']))
{
$x = explode('/', $this->routes['404_override']);
$this->set_directory('');
$this->set_class($x[0]);
$this->set_method(isset($x[1]) ? $x[1] : 'index');
return $x;
}
else
{
show_404($this->fetch_directory().$segments[0]);
}
}
}
else
{
// 如果$segments数组为空,走默认路由
if (strpos($this->default_controller, '/') !== FALSE)
{
$x = explode('/', $this->default_controller);
$this->set_class($x[0]);
$this->set_method($x[1]);
}
else
{
$this->set_class($this->default_controller);
$this->set_method('index');
}
// 如果default controller文件都不存在返回空数组
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.'.php'))
{
$this->directory = '';
return array();
}
}
//如果控制器文件真在子目录中,则返回不包含目录的segments数组
return $segments;
}
// 来到这一步说明,控制器文件不存在,该报404了,有限调用404_override的404页面
if ( ! empty($this->routes['404_override']))
{
$x = explode('/', $this->routes['404_override']);
$this->set_class($x[0]);
$this->set_method(isset($x[1]) ? $x[1] : 'index');
return $x;
}
// Nothing else to do at this point but show a 404
show_404($segments[0]);
}
// --------------------------------------------------------------------
/**
* config/routes.php中路由重定向
*/
function _parse_routes()
{
// 将uri的setment数组转换为uri_string
$uri = implode('/', $this->uri->segments);
// routes.php是否重置该路由
if (isset($this->routes[$uri]))
{
return $this->_set_request(explode('/', $this->routes[$uri]));
}
// Loop through the route array looking for wild-cards
foreach ($this->routes as $key => $val)
{
// CI自定义的:num和:any通配符
$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
// Does the RegEx match?
if (preg_match('#^'.$key.'$#', $uri))
{
// 正则支持逆向应用类似:$route['products/([a-z]+)/(\d+)'] = "$1/id_$2";
if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE)
{
$val = preg_replace('#^'.$key.'$#', $val, $uri);
}
//重定向后的二路由
return $this->_set_request(explode('/', $val));
}
}
// 走到这一步说明没有匹配到重定向路由
$this->_set_request($this->uri->segments);
}
function set_class($class)
{
$this->class = str_replace(array('/', '.'), '', $class);
}
function fetch_class()
{
return $this->class;
}
function set_method($method)
{
$this->method = $method;
}
function fetch_method()
{
if ($this->method == $this->fetch_class())
{
return 'index';
}
return $this->method;
}
function set_directory($dir)
{
$this->directory = str_replace(array('/', '.'), '', $dir).'/';
}
function fetch_directory()
{
return $this->directory;
}
function _set_overrides($routing)
{
if ( ! is_array($routing))
{
return;
}
if (isset($routing['directory']))
{
$this->set_directory($routing['directory']);
}
if (isset($routing['controller']) AND $routing['controller'] != '')
{
$this->set_class($routing['controller']);
}
if (isset($routing['function']))
{
$routing['function'] = ($routing['function'] == '') ? 'index' : $routing['function'];
$this->set_method($routing['function']);
}
}
}
转载注明地址: http://www.phpddt.com/php/ci-router.html 尊重他人劳动成果就是尊重自己!