?
This document uses PHP Chinese website manual Release
URI類提供了幫助您從URI字符串中檢索信息的方法。如果使用URI路由,還可以檢索有關(guān)重路由段的信息。
注
此類由系統(tǒng)自動(dòng)初始化,因此不需要手動(dòng)進(jìn)行。
class CI_URIsegment($n[, $no_result = NULL])
參數(shù): | $ n(int) - 段索引號(hào)$ no_result(混合) - 如果未找到搜索到的段,返回什么 |
---|---|
返回: | 如果未找到分段值或$ no_result值 |
返回類型: | mixed |
$ n(int) - 段索引號(hào)
$ no_result(mixed) - 如果搜索到的段沒(méi)有找到,返回什么
Returns: Segment value or $no\_result value if not found
Return type: mixed
允許您檢索特定的細(xì)分。其中n是您希望檢索的段號(hào)。分段從左到右編號(hào)。例如,如果您的完整URL是這樣的:
片段編號(hào)如下:
新聞
本地
metro
crime_is_up
可選的第二個(gè)參數(shù)默認(rèn)為NULL,并允許您在請(qǐng)求的URI段丟失時(shí)設(shè)置此方法的返回值。例如,這將告訴方法在發(fā)生故障時(shí)返回?cái)?shù)字0:
$ product_id = $ this-> uri-> segment(3,0);
它有助于避免編寫(xiě)這樣的代碼:
if($ this-> uri-> segment(3)=== FALSE){$ product_id = 0; } else {$ product_id = $ this-> uri-> segment(3); }
rsegment($n[, $no_result = NULL])
參數(shù): | $ n(int) - 段索引號(hào)$ no_result(混合) - 如果未找到搜索到的段,返回什么 |
---|---|
返回: | 路由段值或$ no_result值如果未找到 |
返回類型: | 雜 |
$ n(int) - 段索引號(hào)
$ no_result(mixed) - 如果搜索到的段沒(méi)有找到,返回什么
返回:路由段值或$ no \ _result值,如果未找到
Return type: mixed
除了在使用CodeIgniter的[URI路由](../ general / routing)特性的情況下,它允許您從重定路由的URI中檢索特定的段時(shí),該方法與`segment()`相同。
slash_segment($n[, $where = 'trailing'])
參數(shù): | $ n(int) - 段索引號(hào)$ where(string) - 在哪里添加斜線('trailing'或'leading') |
---|---|
返回: | 分段值,前綴/后綴正斜杠,或者如果未找到斜線 |
返回類型: | 串 |
$ n(int) - 段索引號(hào)
$ where(string) - 在哪里添加斜線('trailing'或'leading')
返回:段值,前/后加正斜杠,或者如果未找到斜線
返回類型:字符串
除了基于第二個(gè)參數(shù)添加尾部和/或前導(dǎo)斜杠之外,該方法幾乎與`segment()`相同。如果該參數(shù)未使用,則添加尾部斜線。例子:
$這個(gè) - > URI-> slash_segment(3); $ this-> uri-> slash_segment(3,'leading'); $ this-> uri-> slash_segment(3,'both');
返回:
分割/
/分割
/部分/
slash_rsegment($n[, $where = 'trailing'])
參數(shù): | $ n(int) - 段索引號(hào)$ where(string) - 在哪里添加斜線('trailing'或'leading') |
---|---|
返回: | 路由段值,前綴/后綴正斜杠,或者如果未找到斜線 |
返回類型: | 串 |
$ n(int) - 段索引號(hào)
$ where(string) - 在哪里添加斜線('trailing'或'leading')
返回:路由段值,前綴/后綴正斜杠,或者未找到斜線
Return type: string
This method is identical to `slash_segment()`, except that it lets you add slashes a specific segment from your re-routed URI in the event you are using CodeIgniter’s [URI Routing](../general/routing) feature.
uri_to_assoc([$n = 3[, $default = array()]])
參數(shù): | $ n(int) - 段索引號(hào)$ default(array) - 默認(rèn)值 |
---|---|
返回: | 關(guān)聯(lián)URI段數(shù)組 |
返回類型: | 排列 |
$ n(int) - 段索引號(hào)
$ default(array) - 默認(rèn)值
返回:關(guān)聯(lián)URI段數(shù)組
Return type: array
此方法可讓您將URI段轉(zhuǎn)換為關(guān)鍵字/值對(duì)的關(guān)聯(lián)數(shù)組??紤]這個(gè)URI:
的index.php /用戶/搜索/名/喬/位置/英國(guó)/性別/男性
使用此方法,您可以將URI轉(zhuǎn)換為與此原型關(guān)聯(lián)的數(shù)組:
數(shù)組
第一個(gè)參數(shù)允許您設(shè)置一個(gè)偏移量,它默認(rèn)為3,因?yàn)槟腢RI通常會(huì)在三段中包含一個(gè)控制器/方法對(duì)。例子:
$ array = $ this-> uri-> uri_to_assoc(3); echo $ array'name';
第二個(gè)參數(shù)允許您設(shè)置默認(rèn)鍵名,這樣返回的數(shù)組將始終包含預(yù)期的索引,即使URI中缺少索引。例子:
$ default = array('name','gender','location','type','sort'); $ array = $ this-> uri-> uri_to_assoc(3,$ default);
如果URI在默認(rèn)情況下不包含值,則數(shù)組索引將設(shè)置為該名稱,值為NULL。
最后,如果找不到給定鍵的對(duì)應(yīng)值(如果有奇數(shù)個(gè)URI段),則該值將被設(shè)置為NULL。
ruri_to_assoc([$n = 3[, $default = array()]])
參數(shù): | $ n(int) - 段索引號(hào)$ default(array) - 默認(rèn)值 |
---|---|
返回: | 關(guān)聯(lián)路由URI段數(shù)組 |
返回類型: | 數(shù)組 |
$ n(int) - 段索引號(hào)
$ default(array) - 默認(rèn)值
返回:關(guān)聯(lián)路由URI段數(shù)組
Return type: array
This method is identical to `uri_to_assoc()`, except that it creates an associative array using the re-routed URI in the event you are using CodeIgniter’s [URI Routing](../general/routing) feature.
assoc_to_uri($array)
參數(shù): | $ array(array) - 輸入鍵/值對(duì)的數(shù)組 |
---|---|
返回: | URI字符串 |
返回類型: | 串 |
$數(shù)組%28列陣%29-鍵/值對(duì)的輸入數(shù)組
Returns: URI string
Return type: string
Takes an associative array as input and generates a URI string from it. The array keys will be included in the string. Example:
$Array=數(shù)組%28%27 Products%27=>%27 Shoes%27,%27 size%27%27,%27 color%27=>%27 red%27%29$str=$this->uri->assoc[醫(yī)]到[醫(yī)]URI%28$Array%29;//產(chǎn)品:產(chǎn)品/鞋/尺寸/大/顏色/紅色
uri_string()
Returns: | URI string |
---|---|
Return type: | string |
ruri_string()
Returns: | Routed URI string |
---|---|
Return type: | string |
total_segments()
Returns: | Count of URI segments |
---|---|
Return type: | int |
total_rsegments()
Returns: | Count of routed URI segments |
---|---|
Return type: | int |
segment_array()
Returns: | URI segments array |
---|---|
Return type: | array |
rsegment_array()
Returns: | Routed URI segments array |
---|---|
Return type: | array |
? 2014–2017 British Columbia Institute of Technology
根據(jù)麻省理工學(xué)院的許可。
https://www.codeigniter.com/user[醫(yī)]指南/圖書(shū)館/uri.html