# URI 類
URI 類用于幫助你從 URI 字符串中獲取信息,如果你使用 URI 路由, 你也可以從路由后的 URI 中獲取信息。
注解
該類由系統自己加載,無需手工加載。
* [類參考](http://codeigniter.org.cn/user_guide/libraries/uri.html#id1)
## [類參考](http://codeigniter.org.cn/user_guide/libraries/uri.html#id2)
classCI_URI
segment($n[,?$no_result = NULL])
參數:
* **$n**?(int) -- Segment index number
* **$no_result**?(mixed) -- What to return if the searched segment is not found
返回: Segment value or $no_result value if not found
返回類型: mixed
用于從 URI 中獲取指定段。參數 n 為你希望獲取的段序號,URI 的段從左到右進行編號。 例如,如果你的完整 URL 是這樣的:
~~~
http://example.com/index.php/news/local/metro/crime_is_up
~~~
那么你的每個分段如下:
~~~
#. news
#. local
#. metro
#. crime_is_up
~~~
第二個參數為可選的,默認為 NULL ,它用于設置當所請求的段不存在時的返回值。 例如,如下代碼在失敗時將返回數字 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])
參數:
* **$n**?(int) -- Segment index number
* **$no_result**?(mixed) -- What to return if the searched segment is not found
返回: Routed segment value or $no_result value if not found
返回類型: mixed
當你使用 CodeIgniter 的?[URI 路由](http://codeigniter.org.cn/user_guide/general/routing.html)?功能時,該方法和?segment()?類似, 只是它用于從路由后的 URI 中獲取指定段。
slash_segment($n[,?$where = 'trailing'])
參數:
* **$n**?(int) -- Segment index number
* **$where**?(string) -- Where to add the slash ('trailing' or 'leading')
返回: Segment value, prepended/suffixed with a forward slash, or a slash if not found
返回類型: string
該方法和?segment()?類似,只是它會根據第二個參數在返回結果的前面或/和后面添加斜線。 如果第二個參數未設置,斜線會添加到后面。例如:
~~~
$this->uri->slash_segment(3);
$this->uri->slash_segment(3, 'leading');
$this->uri->slash_segment(3, 'both');
~~~
返回結果:
1. segment/
2. /segment
3. /segment/
slash_rsegment($n[,?$where = 'trailing'])
參數:
* **$n**?(int) -- Segment index number
* **$where**?(string) -- Where to add the slash ('trailing' or 'leading')
返回: Routed segment value, prepended/suffixed with a forward slash, or a slash if not found
返回類型: string
當你使用 CodeIgniter 的?[URI 路由](http://codeigniter.org.cn/user_guide/general/routing.html)?功能時,該方法和?slash_segment()?類似, 只是它用于從路由后的 URI 返回結果的前面或/和后面添加斜線。
uri_to_assoc([$n = 3[,?$default = array()]])
參數:
* **$n**?(int) -- Segment index number
* **$default**?(array) -- Default values
返回: Associative URI segments array
返回類型: array
該方法用于將 URI 的段轉換為一個包含鍵值對的關聯數組。如下 URI:
~~~
index.php/user/search/name/joe/location/UK/gender/male
~~~
使用這個方法你可以將 URI 轉為如下的數組原型:
~~~
[array]
(
'name' => 'joe'
'location' => 'UK'
'gender' => 'male'
)
~~~
你可以通過第一個參數設置一個位移,默認值為 3 ,這是因為你的 URI 的前兩段通常都是控制器和方法。 例如:
~~~
$array = $this->uri->uri_to_assoc(3);
echo $array['name'];
~~~
第二個參數用于設置默認的鍵名,這樣即使 URI 中缺少某個鍵名,也能保證返回的數組中包含該索引。 例如:
~~~
$default = array('name', 'gender', 'location', 'type', 'sort');
$array = $this->uri->uri_to_assoc(3, $default);
~~~
如果某個你設置的默認鍵名在 URI 中不存在,數組中的該索引值將設置為 NULL 。
另外,如果 URI 中的某個鍵沒有相應的值與之對應(例如 URI 的段數為奇數), 數組中的該索引值也將設置為 NULL 。
ruri_to_assoc([$n = 3[,?$default = array()]])
參數:
* **$n**?(int) -- Segment index number
* **$default**?(array) -- Default values
返回: Associative routed URI segments array
返回類型: array
當你使用 CodeIgniter 的?[URI 路由](http://codeigniter.org.cn/user_guide/general/routing.html)?功能時,該方法和?uri_to_assoc()?類似, 只是它用于將路由后的 URI 的段轉換為一個包含鍵值對的關聯數組。
assoc_to_uri($array)
參數:
* **$array**?(array) -- Input array of key/value pairs
返回: URI string
返回類型: string
根據輸入的關聯數組生成一個 URI 字符串,數組的鍵將包含在 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
返回一個相對的 URI 字符串,例如,如果你的完整 URL 為:
~~~
http://example.com/index.php/news/local/345
~~~
該方法返回:
~~~
news/local/345
~~~
ruri_string()
返回: Routed URI string
返回類型: string
當你使用 CodeIgniter 的?[URI 路由](http://codeigniter.org.cn/user_guide/general/routing.html)?功能時,該方法和?uri_string()?類似, 只是它用于返回路由后的 URI 。
total_segments()
返回: Count of URI segments
返回類型: int
返回 URI 的總段數。
total_rsegments()
返回: Count of routed URI segments
返回類型: int
當你使用 CodeIgniter 的?[URI 路由](http://codeigniter.org.cn/user_guide/general/routing.html)?功能時,該方法和?total_segments()?類似, 只是它用于返回路由后的 URI 的總段數。
segment_array()
返回: URI segments array
返回類型: array
返回 URI 所有的段組成的數組。例如:
~~~
$segs = $this->uri->segment_array();
foreach ($segs as $segment)
{
echo $segment;
echo '<br />';
}
~~~
rsegment_array()
返回: Routed URI segments array
返回類型: array
當你使用 CodeIgniter 的?[URI 路由](http://codeigniter.org.cn/user_guide/general/routing.html)?功能時,該方法和?segment_array()?類似, 只是它用于返回路由后的 URI 的所有的段組成的數組。
- 歡迎使用 CodeIgniter
- 安裝說明
- 下載 CodeIgniter
- 安裝說明
- 從老版本升級
- 疑難解答
- CodeIgniter 概覽
- CodeIgniter 將從這里開始
- CodeIgniter 是什么?
- 支持特性
- 應用程序流程圖
- 模型-視圖-控制器
- 設計與架構目標
- 教程 - 內容提要
- 加載靜態內容
- 讀取新聞條目
- 創建新聞條目
- 結束語
- 常規主題
- CodeIgniter URL
- 控制器
- 保留名稱
- 視圖
- 模型
- 輔助函數
- 使用 CodeIgniter 類庫
- 創建類庫
- 使用 CodeIgniter 驅動器
- 創建驅動器
- 創建核心系統類
- 創建附屬類
- 鉤子 - 擴展框架核心
- 自動加載資源
- 公共函數
- 兼容性函數
- URI 路由
- 錯誤處理
- 網頁緩存
- 程序分析
- 以 CLI 方式運行
- 管理你的應用程序
- 處理多環境
- 在視圖文件中使用 PHP 替代語法
- 安全
- PHP 開發規范
- 類庫參考
- 基準測試類
- 緩存驅動器
- 日歷類
- 購物車類
- 配置類
- Email 類
- 加密類
- 加密類(新版)
- 文件上傳類
- 表單驗證類
- FTP 類
- 圖像處理類
- 輸入類
- Javascript 類
- 語言類
- 加載器類
- 遷移類
- 輸出類
- 分頁類
- 模板解析類
- 安全類
- Session 類
- HTML 表格類
- 引用通告類
- 排版類
- 單元測試類
- URI 類
- 用戶代理類
- XML-RPC 與 XML-RPC 服務器類
- Zip 編碼類
- 數據庫參考
- 數據庫快速入門: 示例代碼
- 數據庫配置
- 連接你的數據庫
- 查詢
- 生成查詢結果
- 查詢輔助函數
- 查詢構造器類
- 事務
- 數據庫元數據
- 自定義函數調用
- 數據庫緩存類
- 數據庫工廠類
- 數據庫工具類
- 數據庫驅動器參考
- 輔助函數參考
- 數組輔助函數
- 驗證碼輔助函數
- Cookie 輔助函數
- 日期輔助函數
- 目錄輔助函數
- 下載輔助函數
- 郵件輔助函數
- 文件輔助函數
- 表單輔助函數
- HTML 輔助函數
- 語言輔助函數
- Inflector 輔助函數
- 數字輔助函數
- 路徑輔助函數
- 安全輔助函數
- 表情輔助函數
- 字符串輔助函數
- 文本輔助函數
- 排版輔助函數
- URL 輔助函數
- XML 輔助函數
- 向 CodeIgniter 貢獻你的力量