|
CodeIgniter 号称生成的 URL 非常干净而且是对搜索引擎友好化的。不同于标准的“字符串查询”方法
1 example.com/news/article/345
2 example.com?c=news&m=article&id=345
上面是于enable_query_strings在不同环境下的URL
如果我要获取上面URL里的345,当然用CI的$this->uri->segment ( 3 ),PHP自身的$_GET一样可以,
但是,我现在要做一个项目,不因为enable_query_strings的改变一会儿$_GET一会儿$this->uri->segment ( 3 )的,
在说没有病一会儿判断$this->uri->segment ( 3 )一会儿判断$_GET的,没必要吧
那么我想在整个项目里只用$this->uri->segment ( 3 ),此时有只扩展segment
文件 system/core/URL.php
segment函数如下:
PHP复制代码 function segment ($n, $no_result = FALSE)
{
return ( ! isset($this->segments[$n])) ? $no_result : $this->segments[$n];
} 复制代码
扩展segment如下
PHP复制代码 function segment ($n, $no_result = FALSE) {
if ($this->config->item ( 'enable_query_strings' ) == FALSE) {
return (! isset ( $this->segments [$n] )) ? $no_result : $this->segments [$n];
} else {
$getarray = array ('1' => 'c', 2 => 'm', '3' => 'id', '4' => 'name' );
$get = $_GET;
return (! isset ( $get [$getarray [$n]] )) ? $no_result : $get [$getarray [$n]];
}
} 复制代码
那么仅仅扩展segment是不够的.在扩展site_url,site_url是CI核心site_url,不是辅助函数的site_url
文件 system/core/Config.php
site_url函数原型
PHP复制代码 function site_url($uri = '')
{
if ($uri == '')
{
return $this->slash_item('base_url').$this->item('index_page');
}
if ($this->item('enable_query_strings') == FALSE)
{
$suffix = ($this->item('url_suffix') == FALSE) ? '' : $this->item('url_suffix');
return $this->slash_item('base_url').$this->slash_item('index_page').$this->_uri_string($uri).$suffix;
}
else
{
return $this->slash_item('base_url').$this->item('index_page').'?'.$this->_uri_string($uri);
}
} 复制代码
扩展后的site_url
PHP复制代码 function site_url ($uri = '') {
if ($uri == '') {
return $this->slash_item ( 'base_url' ) . $this->item ( 'index_page' );
}
if ($this->item ( 'enable_query_strings' ) == FALSE) {
$suffix = ($this->item ( 'url_suffix' ) == FALSE) ? '' : $this->item ( 'url_suffix' );
return $this->slash_item ( 'base_url' ) . $this->slash_item ( 'index_page' ) . $this->_uri_string ( $uri ) . $suffix;
} else {
$str = $this->_uri_string ( $uri );
$str = 'c=' . $str;
$f = stripos ( $str, '/' );
if ($f) {
$as = substr ( $str, 0, $f );
$fs = '&m=' . substr ( $str, $f + 1 );
$str = $as . $fs;
}
$i = stripos ( $str, '/' );
if ($i) {
$bs = substr ( $str, 0, $i );
$is = '&id=' . substr ( $str, $i + 1 );
$str = $bs . $is;
}
$n = stripos ( $str, '/' );
if ($n) {
$cs = substr ( $str, 0, $n );
$ns = '&name=' . substr ( $str, $n + 1 );
$str = $cs . $ns;
}
//$str = str_replace ( '?', '&', $str );
return $this->slash_item ( 'base_url' ) . $this->item ( 'index_page' ) . '?' . $this->_uri_string ( $str );
}
} 复制代码
此时$this->uri->segment ( 3 )就可以通用了
后记:
为了更强大.我们应该在配置文件cofnig里定义一个这样
$config['XXXX']='index.php|c|m|i|f|p';
在URL.hph里
PHP复制代码 function segment ($n, $no_result = FALSE) {
if ($this->config->item ( 'enable_query_strings' ) == FALSE) {
return (! isset ( $this->segments [$n] )) ? $no_result : $this->segments [$n];
}
$getarray = explode ('|',$this->config->item ( 'XXXX' ));
$get = $_GET;
return (! isset ( $get [$getarray [$n]] )) ? $no_result : $get [$getarray [$n]];
} 复制代码
site_url自己去更强大吧
一般用的时候都是加载辅助函数site_url,然后在视图里就
PHP复制代码 <?php echo site_url('/文章/查看/文章ID');?> 复制代码
然后根据enable_query_strings环境生成以下
index.php/文章/查看/文章ID
index.php?c=文章&m=查看&id=文章ID |
评分
-
查看全部评分
|