W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
URI 類用于幫助你從 URI 字符串中獲取信息,如果你使用 URI 路由, 你也可以從路由后的 URI 中獲取信息。
注解
該類由系統(tǒng)自己加載,無(wú)需手工加載。
classCI_URI
segment($n[, $no_result = NULL])
參數(shù):
返回: Segment value or $no_result value if not found
返回類型: mixed
用于從 URI 中獲取指定段。參數(shù) n 為你希望獲取的段序號(hào),URI 的段從左到右進(jìn)行編號(hào)。 例如,如果你的完整 URL 是這樣的:
http://example.com/index.php/news/local/metro/crime_is_up
那么你的每個(gè)分段如下:
#. news
#. local
#. metro
#. crime_is_up
第二個(gè)參數(shù)為可選的,默認(rèn)為 NULL ,它用于設(shè)置當(dāng)所請(qǐng)求的段不存在時(shí)的返回值。 例如,如下代碼在失敗時(shí)將返回?cái)?shù)字 0
$product_id = $this->uri->segment(3, 0);
它可以避免你寫出類似于下面這樣的代碼:
if ($this->uri->segment(3) === FALSE)
{
$product_id = 0;
}
else
{
$product_id = $this->uri->segment(3);
}
rsegment($n[, $no_result = NULL])
參數(shù):
返回: Routed segment value or $no_result value if not found
返回類型: mixed
當(dāng)你使用 CodeIgniter 的 URI 路由 功能時(shí),該方法和 segment() 類似, 只是它用于從路由后的 URI 中獲取指定段。
slash_segment($n[, $where = 'trailing'])
參數(shù):
返回: Segment value, prepended/suffixed with a forward slash, or a slash if not found
返回類型: string
該方法和 segment() 類似,只是它會(huì)根據(jù)第二個(gè)參數(shù)在返回結(jié)果的前面或/和后面添加斜線。 如果第二個(gè)參數(shù)未設(shè)置,斜線會(huì)添加到后面。例如:
$this->uri->slash_segment(3);
$this->uri->slash_segment(3, 'leading');
$this->uri->slash_segment(3, 'both');
返回結(jié)果:
slash_rsegment($n[, $where = 'trailing'])
參數(shù):
返回: Routed segment value, prepended/suffixed with a forward slash, or a slash if not found
返回類型: string
當(dāng)你使用 CodeIgniter 的 URI 路由 功能時(shí),該方法和 slash_segment() 類似, 只是它用于從路由后的 URI 返回結(jié)果的前面或/和后面添加斜線。
uri_to_assoc([$n = 3[, $default = array()]])
參數(shù):
返回: Associative URI segments array
返回類型: array
該方法用于將 URI 的段轉(zhuǎn)換為一個(gè)包含鍵值對(duì)的關(guān)聯(lián)數(shù)組。如下 URI:
index.php/user/search/name/joe/location/UK/gender/male
使用這個(gè)方法你可以將 URI 轉(zhuǎn)為如下的數(shù)組原型:
[array]
(
'name' => 'joe'
'location' => 'UK'
'gender' => 'male'
)
你可以通過(guò)第一個(gè)參數(shù)設(shè)置一個(gè)位移,默認(rèn)值為 3 ,這是因?yàn)槟愕?URI 的前兩段通常都是控制器和方法。 例如:
$array = $this->uri->uri_to_assoc(3);
echo $array['name'];
第二個(gè)參數(shù)用于設(shè)置默認(rèn)的鍵名,這樣即使 URI 中缺少某個(gè)鍵名,也能保證返回的數(shù)組中包含該索引。 例如:
$default = array('name', 'gender', 'location', 'type', 'sort');
$array = $this->uri->uri_to_assoc(3, $default);
如果某個(gè)你設(shè)置的默認(rèn)鍵名在 URI 中不存在,數(shù)組中的該索引值將設(shè)置為 NULL 。
另外,如果 URI 中的某個(gè)鍵沒有相應(yīng)的值與之對(duì)應(yīng)(例如 URI 的段數(shù)為奇數(shù)), 數(shù)組中的該索引值也將設(shè)置為 NULL 。
ruri_to_assoc([$n = 3[, $default = array()]])
參數(shù):
返回: Associative routed URI segments array
返回類型: array
當(dāng)你使用 CodeIgniter 的 URI 路由 功能時(shí),該方法和 uri_to_assoc() 類似, 只是它用于將路由后的 URI 的段轉(zhuǎn)換為一個(gè)包含鍵值對(duì)的關(guān)聯(lián)數(shù)組。
assoc_to_uri($array)
參數(shù):
返回: URI string
返回類型: string
根據(jù)輸入的關(guān)聯(lián)數(shù)組生成一個(gè) URI 字符串,數(shù)組的鍵將包含在 URI 的字符串中。例如:
$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');
$str = $this->uri->assoc_to_uri($array);
// Produces: product/shoes/size/large/color/red
uri_string()
返回: URI string
返回類型: string
返回一個(gè)相對(duì)的 URI 字符串,例如,如果你的完整 URL 為:
http://example.com/index.php/news/local/345
該方法返回:
news/local/345
ruri_string()
返回: Routed URI string
返回類型: string
當(dāng)你使用 CodeIgniter 的 URI 路由 功能時(shí),該方法和 uri_string() 類似, 只是它用于返回路由后的 URI 。
total_segments()
返回: Count of URI segments
返回類型: int
返回 URI 的總段數(shù)。
total_rsegments()
返回: Count of routed URI segments
返回類型: int
當(dāng)你使用 CodeIgniter 的 URI 路由 功能時(shí),該方法和 total_segments() 類似, 只是它用于返回路由后的 URI 的總段數(shù)。
segment_array()
返回: URI segments array
返回類型: array
返回 URI 所有的段組成的數(shù)組。例如:
$segs = $this->uri->segment_array();
foreach ($segs as $segment)
{
echo $segment;
echo '<br />';
}
rsegment_array()
返回: Routed URI segments array
返回類型: array
當(dāng)你使用 CodeIgniter 的 URI 路由 功能時(shí),該方法和 segment_array() 類似, 只是它用于返回路由后的 URI 的所有的段組成的數(shù)組。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: