Router 与 URI 类联系紧密
URI类的解释参见 URI类
CodeIgniter.php 里面调用的是 Router 的_set_routing
Router 里面调用 URI的 _fetch_uri_string
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class CI_Router {
var $config;
var $routes = array();
var $error_routes = array();
var $class = '';
var $method = 'index';
var $directory = '';
var $default_controller;
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();//请求参数部分的片段
// 允许以GET方式访问 且 设置了配置文件中的controller_trigger
if ($this->config->item('enable_query_strings') === TRUE AND isset($_GET[$this->config->item('controller_trigger')]))
{
// 如果GET数组里面有选项 、set_directory 设置目录 、_filter_uri 过滤字符串 fetch_directory 设置目录到 数组 segments
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();
}
}
/*
加载配置文件 config/routes.php
并且赋值给 this->routes
*/
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);
// 设置默认controller请求
$this->default_controller = ( ! isset($this->routes['default_controller']) OR $this->routes['default_controller'] == '') ? FALSE : strtolower($this->routes['default_controller']);
if (count($segments) > 0)
{
// 只有GET请求并且不是默认请求的话这里 segments>0
// 默认请求 PATH_INFO 形式的请求后不会执行这里
return $this->_validate_request($segments);
}
// 获取uri_string 这里开始调用URI类
$this->uri->_fetch_uri_string();
if ($this->uri->uri_string == '')
{
//uri_string 为空用设置控制器为默认controller
return $this->_set_default_controller();
}
// 去除uri后最
$this->uri->_remove_url_suffix();
//对PATH_INFO请求的uri参数进行分解
$this->uri->_explode_segments();
//这个函数的调用很重要 将uri类中的处理结果搬到Router
$this->_parse_routes();
$this->uri->_reindex_segments();
}
// 设置默认controller请求
function _set_default_controller()
{
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.");
}
if (strpos($this->default_controller, '/') !== FALSE)
{
// controller/method 模式
$x = explode('/', $this->default_controller);
$this->set_class($x[0]);
$this->set_method($x[1]);
$this->_set_request($x);//设置请求
}
else
{
// controller模式
$this->set_class($this->default_controller);
$this->set_method('index');//默认方法index
$this->_set_request(array($this->default_controller, 'index'));
}
$this->uri->_reindex_segments();
log_message('debug', "No URI present. Default controller set.");
}
/*
设置控制器的 class method
最主要的是最后一句为URI 的 resegments 赋值
*/
function _set_request($segments = array())
{
// 检测 segments
$segments = $this->_validate_request($segments);
if (count($segments) == 0)
{//segments为空 设置默认controller
return $this->_set_default_controller();
}
$this->set_class($segments[0]);
if (isset($segments[1]))
{
$this->set_method($segments[1]);
}
else
{
$segments[1] = 'index';
}
// 更新segments内容到 rsegments
$this->uri->rsegments = $segments;
}
/*
过滤主要是通过 direct class method 这些检测class文件是否存在
检测成功(文件存在)返回segments数组
失败 show_error()
*/
function _validate_request($segments)
{
if (count($segments) == 0)
{
return $segments;//空
}
if (file_exists(APPPATH.'controllers/'.$segments[0].'.php'))
{
return $segments; //没有目录directory 直接检测class文件是否存在
}
// 若存在目录
if (is_dir(APPPATH.'controllers/'.$segments[0]))
{
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);//将direct从数组中去除
if (count($segments) > 0)
{
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
{//use default controller string
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();
}
}
return $segments;
}
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;
}
show_404($segments[0]);
}
/**
* This function matches any routes that may exist in
* the config/routes.php file against the URI to
* determine if the class/method need to be remapped.
*/
function _parse_routes()
{
$uri = implode('/', $this->uri->segments);//拼接
if (isset($this->routes[$uri]))
{
return $this->_set_request(explode('/', $this->routes[$uri]));
}
foreach ($this->routes as $key => $val)
{
// 通过正则匹配route到指定的uri
$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
if (preg_match('#^'.$key.'$#', $uri))
{
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;
}
// 通过index文件指定的routing数组传递到这里进行内存覆盖
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']);
}
}
}
总结:
大致功能简介
_set_routing 加载Router配置文件 设置http请求的路由信息 directory class method
_set_defuault_controller 设置默认的路由信息
_set_request 设置请求路由 并会将解析出来的segment数组赋值给URI类的resegment属性
_set_validate_rerquest 检测路由 检测文件http请求的文件是否存在
_parse_routes 对uri进行检测匹配
Code Tips:
1、_set_routing 里面通过GET数组设置directory class method 等路由信息的方法
感觉_set_request函数写的有点啰嗦 除了最后一句赋值