?
Ce document utilise Manuel du site Web PHP chinois Libérer
Url 幫助類(lèi)提供一系列的靜態(tài)方法來(lái)幫助管理 URL。
有兩種獲取通用 URLS 的方法 :當(dāng)前請(qǐng)求的 home URL 和 base URL 。 為了獲取 home URL ,使用如下代碼:
$relativeHomeUrl = Url::home();
$absoluteHomeUrl = Url::home(true);
$httpsAbsoluteHomeUrl = Url::home('https');
如果沒(méi)有傳任何參數(shù),這個(gè)方法將會(huì)生成相對(duì) URL 。你可以傳?true
?來(lái)獲得一個(gè)針對(duì)當(dāng)前協(xié)議的絕對(duì) URL; 或者,你可以明確的指定具體的協(xié)議類(lèi)型(?https
?,?http
?)。
如下代碼可以獲得當(dāng)前請(qǐng)求的 base URL:
`
php $relativeBaseUrl = Url::base(); $absoluteBaseUrl = Url::base(true); $httpsAbsoluteBaseUrl = Url::base('https');?`
這個(gè)方法的調(diào)用方式和?Url::home()
?的完全一樣。
為了創(chuàng)建一個(gè)給定路由的 URL 地址,請(qǐng)使用?Url::toRoute()
方法。 這個(gè)方法使用 \yii\web\UrlManager 來(lái)創(chuàng)建一個(gè) URL :
$url = Url::toRoute(['product/view', 'id' => 42]);
你可以指定一個(gè)字符串來(lái)作為路由,如:?site/index
?。如果想要指定將要被創(chuàng)建的 URL 的附加查詢(xún)參數(shù), 你同樣可以使用一個(gè)數(shù)組來(lái)作為路由。數(shù)組的格式須為:
// generates: /index.php?r=site/index¶m1=value1¶m2=value2
['site/index', 'param1' => 'value1', 'param2' => 'value2']
如果你想要?jiǎng)?chuàng)建一個(gè)帶有 anchor 的 URL ,你可以使用一個(gè)帶有?#
?參數(shù)的數(shù)組。比如:
// generates: /index.php?r=site/index¶m1=value1#name
['site/index', 'param1' => 'value1', '#' => 'name']
一個(gè)路由既可能是絕對(duì)的又可能是相對(duì)的。一個(gè)絕對(duì)的路由以前導(dǎo)斜杠開(kāi)頭(如:?/site/index
), 而一個(gè)相對(duì)的路由則沒(méi)有(比如:site/index
?或者?index
)。一個(gè)相對(duì)的路由將會(huì)按照如下規(guī)則轉(zhuǎn)換為絕對(duì)路由:
index
?),它會(huì)被認(rèn)為是當(dāng)前控制器的一個(gè) action ID, 然后將會(huì)把 \yii\web\Controller::uniqueId 插入到路由前面。site/index
?),它會(huì)被認(rèn)為是相對(duì)當(dāng)前模塊(module)的路由, 然后將會(huì)把 \yii\base\Module::uniqueId 插入到路由前面。從2.0.2版本開(kāi)始,你可以用?alias?來(lái)指定一個(gè)路由。 在這種情況下, alias 將會(huì)首先轉(zhuǎn)換為實(shí)際的路由, 然后會(huì)按照上述規(guī)則轉(zhuǎn)換為絕對(duì)路由。
以下是該方法的一些例子:
// /index.php?r=site/indexecho Url::toRoute('site/index');
// /index.php?r=site/index&src=ref1#nameecho Url::toRoute(['site/index', 'src' => 'ref1', '#' => 'name']);
// /index.php?r=post/edit&id=100 assume the alias "@postEdit" is defined as "post/edit"echo Url::toRoute(['@postEdit', 'id' => 100]);
// http://www.example.com/index.php?r=site/indexecho Url::toRoute('site/index', true);
// https://www.example.com/index.php?r=site/indexecho Url::toRoute('site/index', 'https');
還有另外一個(gè)方法?Url::to()
?和 toRoute() 非常類(lèi)似。這兩個(gè)方法的唯一區(qū)別在于,前者要求一個(gè)路由必須用數(shù)組來(lái)指定。 如果傳的參數(shù)為字符串,它將會(huì)被直接當(dāng)做 URL 。
Url::to()
?的第一個(gè)參數(shù)可以是:
['site/index']
,?['post/index', 'page' => 2]
?。 詳細(xì)用法請(qǐng)參考 toRoute() 。@
?的字符串:它將會(huì)被當(dāng)做別名, 對(duì)應(yīng)的別名字符串將會(huì)返回。當(dāng)?$scheme
?指定了(無(wú)論是字符串還是 true ),一個(gè)帶主機(jī)信息(通過(guò) \yii\web\UrlManager::hostInfo 獲得) 的絕對(duì) URL 將會(huì)被返回。如果?$url
?已經(jīng)是絕對(duì) URL 了, 它的協(xié)議信息將會(huì)被替換為指定的( https 或者 http )。
以下是一些使用示例:
// /index.php?r=site/indexecho Url::to(['site/index']);
// /index.php?r=site/index&src=ref1#nameecho Url::to(['site/index', 'src' => 'ref1', '#' => 'name']);
// /index.php?r=post/edit&id=100 assume the alias "@postEdit" is defined as "post/edit"echo Url::to(['@postEdit', 'id' => 100]);
// the currently requested URLecho Url::to();
// /images/logo.gifecho Url::to('@web/images/logo.gif');
// images/logo.gifecho Url::to('images/logo.gif');
// http://www.example.com/images/logo.gifecho Url::to('@web/images/logo.gif', true);
// https://www.example.com/images/logo.gifecho Url::to('@web/images/logo.gif', 'https');
從2.0.3版本開(kāi)始,你可以使用 yii\helpers\Url::current() 來(lái)創(chuàng)建一個(gè)基于當(dāng)前請(qǐng)求路由和 GET 參數(shù)的 URL。 你可以通過(guò)傳遞一個(gè)$params
?給這個(gè)方法來(lái)添加或者刪除 GET 參數(shù)。 例如:
// assume $_GET = ['id' => 123, 'src' => 'google'], current route is "post/view"
// /index.php?r=post/view&id=123&src=googleecho Url::current();
// /index.php?r=post/view&id=123echo Url::current(['src' => null]);
// /index.php?r=post/view&id=100&src=googleecho Url::current(['id' => 100]);
有時(shí),你需要記住一個(gè) URL 并在后續(xù)的請(qǐng)求處理中使用它。 你可以用以下方式達(dá)到這個(gè)目的:
// Remember current URL
Url::remember();
// Remember URL specified. See Url::to() for argument format.
Url::remember(['product/view', 'id' => 42]);
// Remember URL specified with a name given
Url::remember(['product/view', 'id' => 42], 'product');
在后續(xù)的請(qǐng)求處理中,可以用如下方式獲得記住的 URL:
$url = Url::previous();
$productUrl = Url::previous('product');
你可以用如下代碼檢測(cè)一個(gè) URL 是否是相對(duì)的(比如,包含主機(jī)信息部分)。
$isRelative?=?Url::isRelative('test/it');