<?php |
// URI组件里面有很多方法,大都是一些辅助作用的方法,而此方法是URI最主线的一个方法。 |
function _fetch_uri_string() |
{ |
//下面的uri_protocol是在config.php里面的一个配置项,其实是问你用哪种方式去检测uri的信息的意思, |
//默认是AUTO,自动检测,也就是通过各种方式检测,直至检测到,或者全部方式都检测完。 |
if ( strtoupper ( $this ->config->item( 'uri_protocol' )) == 'AUTO' ) |
{ |
// Is the request coming from the command line? |
//开始尝试各种方式,主要有:命令行,REQUEST_URI, PATH_INFO, QUERY_STRING. |
//下面会多次出现$this->_set_uri_string($str)这个方法,这个方法没别的,就是把$str经过 |
//过滤和修剪后值给$this->uri_string属性,在这里暂时可以理解为就是赋值。 |
//如果脚本是在命令行模式下运行的话,那么参数就是通过$_SERVER['argv']来传递。下面的 |
//$this->_parse_cli_args();就是拿到符合我们需要的路由相关的一些参数鸟~如果大部分 |
//情况你没用命令行执行脚本的话,下面这个if暂时可以不用管。 |
if (php_sapi_name() == 'cli' or defined( 'STDIN' )) |
{ |
$this ->_set_uri_string( $this ->_parse_cli_args()); |
return ; |
} |
// Let's try the REQUEST_URI first, this will work in most situations |
// 这种REQUEST_URI方式相对复杂一点,因此封装在$this->_detect_uri(); 里面。 |
// 其实大多数情况下,利用REQUEST URI和SCRIPT NAME都会得到我们想要的路径信息了。 |
if ( $uri = $this ->_detect_uri()) |
{ |
$this ->_set_uri_string( $uri ); |
return ; |
} |
// Is there a PATH_INFO variable? |
// Note: some servers seem to have trouble with getenv() so we'll test it two ways |
// PATH_INFO方式,个人觉得这种方式最经济,只是不是每次请求都有$_SERVER['PATH_INFO']这个变量。 |
$path = (isset( $_SERVER [ 'PATH_INFO' ])) ? $_SERVER [ 'PATH_INFO' ] : @ getenv ( 'PATH_INFO' ); |
if (trim( $path , '/' ) != '' && $path != "/" .SELF) |
{ |
$this ->_set_uri_string( $path ); |
return ; |
} |
// No PATH_INFO?... What about QUERY_STRING? |
// 如果是用QUERY_STRING的话,路径格式一般为index.php?/controller/method/xxx/xxx |
$path = (isset( $_SERVER [ 'QUERY_STRING' ])) ? $_SERVER [ 'QUERY_STRING' ] : @ getenv ( 'QUERY_STRING' ); |
if (trim( $path , '/' ) != '' ) |
{ |
$this ->_set_uri_string( $path ); |
return ; |
} |
// As a last ditch effort lets try using the $_GET array |
// 上面的方法都不行,那真是奇怪了。。所以尝试最后一种奇葩的方法,就是从$_GET里面把那个键名拿出来。 |
if ( is_array ( $_GET ) && count ( $_GET ) == 1 && trim(key( $_GET ), '/' ) != '' ) |
{ |
$this ->_set_uri_string(key( $_GET )); |
return ; |
} |
// We've exhausted all our options... |
$this ->uri_string = '' ; |
return ; |
} |
|
// 这里是因为上面那个获得uri_protocol配置的语句写在if里面,然后又没赋值到某个变量,所以这里要再写一次了 |
// 可能是因为大多数情况下,我们都是选择AUTO方式吧。 |
$uri = strtoupper ( $this ->config->item( 'uri_protocol' )); |
|
//其实就是按规定的方式去找路径而已 |
if ( $uri == 'REQUEST_URI' ) |
{ |
$this ->_set_uri_string( $this ->_detect_uri()); |
return ; |
} |
elseif ( $uri == 'CLI' ) |
{ |
$this ->_set_uri_string( $this ->_parse_cli_args()); |
return ; |
} |
|
//如果你在配置文件config.php里面把这个uri_protocol定义成一种上面都没有的方式,那么就会执行下面的代码。 |
//意思是,就看$_SERVER有没有这个uri_protocol的变量了,有就给,没有就算了。 |
$path = (isset( $_SERVER [ $uri ])) ? $_SERVER [ $uri ] : @ getenv ( $uri ); |
$this ->_set_uri_string( $path ); |
} |
?> |