状态 草稿
Todo Needs work on code examples
官方最后更新时间 2008/08/11 06:48

URI 库

URI 类提供 URI 以及 URI 分段的方法。如果您使用的路由(routing )它的方法重新定位到 URI 这里。

注意:

这个类是 Kohana 系统自动加载,不需要自己重新加载。

方法

segment()

segment($index = 1, $default = FALSE) 解析并返回 URI 分段,当分段不存在返回 $default。

//url: http://www.example.com/index.php/article/paris/hilton/

上面 URI 的分度是:

  1. article
  2. paris
  3. hilton
echo $this->uri->segment(3); // 返回 'hilton'
echo $this->uri->segment(4, 'spears'); // 返回 'spears'

注意:这个方法也接受字符串。当字符串作为第一个分段式,他会返回接下来的字符串。

echo $this->uri->segment('article'); // 返回 'paris'
echo $this->uri->segment('paris'); // 返回 'hilton'
echo $this->uri->segment('hilton'); // 返回 FALSE

rsegment()

segment() 方法,但是…..:?:

原文:Identical to segment() except that it uses the rerouted URI to retrieve the segments from.

segment_array()

segment_array($offset,$associative) 返回所有 URL 分段的数组

total_segments()

total_segments() 返回分段数量

echo $this->uri->total_segments(); // 返回 3

string()

string() 当前 URI 作为一个字符串返回

echo $this->uri->string(); // 返回:article/paris/hilton/

last_segment()

last_segment() 返回一个 URI 上最后一个分段

echo $this->uri->last_segment(); // 返回:hilton

argument()

argument() 返回被请求的字段(arguments)。这有和分段有区别,因为它只会获得被请求的字段而跳过控制器和方法分段

echo $this->uri->argument(1); // 返回:hilton

argument_array()

argument_array() 以数组返回包含所有的字段(arguments)

echo $this->uri->argument_array(); // 返回:array( 'hilton' )

total_arguments()

total_arguments() 返回字段(arguments)的总数

echo $this->uri->total_arguments(); // 返回:1

build_array()

build_array($array, $offset = 0, $associative = FALSE) 从数组和偏移量创建一个简单或关联数组,包含了:

  • [array] 数组重建
  • [integer] 开始的偏移量
  • [boolean] 创建一个关联数组(TRUEFALSE,默认是 FALSE

返回从定义偏移量开始的相应数组

实例

print Kohana::debug($this->uri->build_array(array('apple', 'mango', 'pineapple'), 1));
print Kohana::debug($this->uri->build_array(array('fruit1', 'apple', 'fruit2', 'mango', 'fruit3', 'pineapple'), 2, TRUE));

返回结果:

Array
(
    [2] => mango
    [3] => pineapple
)
 
Array
(
    [fruit2] => mango
    [fruit3] => pineapple
)
libraries/uri.txt · 最后更改: 2008/11/18 18:48 由 icyleaf