Uri 类別

Uri 类別能让你與 URI 互动。

base($include_index = true)

base 方法回傳基础 URL,不包括 index_file 當 $include_index 設为 false 時。

請注意:如果在 app/config/config.php 裡 index_file 沒有被設定,$include_index 将被忽略。

靜態
參数
參数 預設 描述
$include_index
true
是否附加索引到回傳 URL。
回傳 字串,基础 URL 與選擇性的索引档案附加
範例
echo Uri::base();
// http://localhost/index.php

echo Uri::base(false);
// http://localhost/

create($uri = null, $variables = array(), $get_variables = array(), $secure = null)

create 方法能让你建立一个 URL 根據所給的 URI,包括基础 URL。

靜態
參数
參数 預設 描述
$uri
null
URL。
$variables
array()
URL 的變数。
$get_variables
array()
添加到 URL 的查詢字串,其值 & 鍵可使用 $variables 陣列中的變数。
$secure
null
設定为 false 以強制 http,或設定为 true 以強制 https 在建立的 URL
回傳 字串
範例
echo Uri::create('controller/method');
// 回傳 http://localhost/controller/method

echo Uri::create('controller/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'));
// 回傳 http://localhost/controller/thing?what=more

echo Uri::create('http://www.example.org/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'));
// 回傳 http://www.example.org/thing?what=more

echo Uri::create('http://www.example.org/:some', array('some' => 'thing', 'and' => 'more'), array('what' => ':and'), true);
// 回傳 https://www.example.org/thing?what=more

current()

current 方法能让你提取目前的 URL,包括在 HMVC 模式的基础 URL。

靜態
參数
回傳 字串
範例
// 範例 URL:http://localhost/module/controller/method
echo Uri::current(); // 回傳 http://localhost/module/controller/method

main()

main 方法能让你提取目前的 URL,包括基础 URL。

靜態
參数
回傳 字串
範例
// 範例 URL:http://localhost/controller/method
echo Uri::main(); // 回傳 http://localhost/controller/method

segment($segment, $default = null)

segment 方法能让你回傳想要的分段,或不存在時回傳 false。

靜態
參数
參数 預設 描述
$segment 必要 分段编號。
$default
null
預設值。
回傳 字串
範例
// 範例 URL:http://localhost/controller/method/param1/param2
echo Uri::segment(3); // 回傳 param1

segments()

segments 方法能让你提取目前的 URI 分段陣列。

靜態
參数
回傳 陣列
範例
// 範例 URL:http://localhost/controller/method
echo Uri::segments(); // 回傳 array(0 => 'controller', 1 => 'method')

segment_replace($url, $secure = null)

segment_replace 方法能让你以目前的 URI 分段,在所給的 URL 中取代該位置的萬用字元。

靜態
參数
參数 預設 描述
$url 必要 包含萬用字元的 URL。萬用字元被定義为一个 * 字元。
$secure
null
如果为 false,回傳的 URL 会使用 HTTP,如果为 true,它会使用 HTTPS。
回傳 字串
拋出 OutOfBoundsException,如果一个萬用字元的位置在片段中不存在任何值。
範例
// 範例:目前 URL = http://localhost/one/two,有片段 'one' 跟 'two'

// 回傳 http://localhost/one/two/three
echo Uri::segment_replace('http://localhost/*/*/three');

// 拋出一个例外,因为沒有可用的第三片段
echo Uri::segment_replace('http://localhost/this/that/*/other');

// 回傳 https://localhost/one/two/three
echo Uri::segment_replace('http://localhost/*/*/three', true);

update_query_string($vars = array(), $uri = null, $secure = null)

update_query_string 方法能让你更新目前或傳遞的 URI 的查詢字串。

靜態
參数
參数 預設 描述
$vars
array()
一个查詢變数的關聯陣列,或一个包含變数名稱的字串。
$uri
null
要使用的 URL。如果沒給,目前的 URL 会被使用。如果 $vars 是一个字串,这会包含變数值 (这意味著在这情況下,你不能傳遞一个自訂的 URI)。
$secure
null
如果为 false,回傳的 URL 会使用 HTTP,如果为 true,它会使用 HTTPS。
回傳 字串
範例
// 例如:Current URL = http://localhost/test?one=1&two=2

// 回傳 http://localhost/test?one=1&two=2&three=3
echo Uri::update_query_string(array('three' => 3));

// 回傳 http://localhost/test?one=1&two=3
echo Uri::update_query_string(array('two' => 3));

// 回傳 http://localhost/controller?four=4
echo Uri::update_query_string(array('four' => 4), 'http://localhost/controller');

// 回傳 https://localhost/test?one=1&two=2&three=3
echo Uri::update_query_string('three', 3, true);

to_assoc($start = 1)

to_assoc 方法使用分段陣列并把它變成一个關聯陣列如果分段数是偶数。如果分段数是奇数,最後的陣列鍵将有一个 null 值。

靜態
參数
參数 預設 描述
$start
1
要開始的分段数。这能让你剝離引導分段。
回傳 陣列
範例
// Uri 是 /welcome/index/page/4
$arr = Uri::to_assoc();
/*
結果:
array(
    'welcome' => 'index',
    'page'    => 4,
);
*/

// Uri 是 /welcome
$arr = Uri::to_assoc();
/*
結果:
array(
    'welcome' => null,
);
*/

string()

string 方法能让你提取目前的 URI。

靜態
參数
回傳 字串
範例
// 範例 URL:http://localhost/controller/method
echo Uri::string(); // 回傳 controller/method

build_query_string($values ...)

build_query_string 方法能让你從一个或更多傳遞的關聯陣列或字串值產生一个查詢字串。 你可以透過 Input::get() 從现有的來建構一个更新的查詢字串。如果你傳遞一个字串,它会被假設为一个開關, 并且会被轉換为 "string=1"。

靜態
參数
參数 預設 描述
$values 必要 一个或更多關聯陣列或字串值
回傳 字串
範例
// 回傳 "varA=varA&varB=1&varC=varC"
echo Uri::build_query_string(array('varA' => 'varA'), 'varB', array('varC' => 'varC'));