W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Url 幫助類提供一系列的靜態(tài)方法來幫助管理 URL。
有兩種獲取通用 URLS 的方法 :當(dāng)前請求的 home URL 和 base URL 。 為了獲取 home URL ,使用如下代碼:
$relativeHomeUrl = Url::home();
$absoluteHomeUrl = Url::home(true);
$httpsAbsoluteHomeUrl = Url::home('https');
如果沒有傳任何參數(shù),這個方法將會生成相對 URL 。你可以傳?true
?來獲得一個針對當(dāng)前協(xié)議的絕對 URL; 或者,你可以明確的指定具體的協(xié)議類型(?https
?,?http
?)。
如下代碼可以獲得當(dāng)前請求的 base URL:
php $relativeBaseUrl = Url::base(); $absoluteBaseUrl = Url::base(true); $httpsAbsoluteBaseUrl = Url::base('https');?
這個方法的調(diào)用方式和?Url::home()
?的完全一樣。
為了創(chuàng)建一個給定路由的 URL 地址,請使用?Url::toRoute()
方法。 這個方法使用 \yii\web\UrlManager 來創(chuàng)建一個 URL :
$url = Url::toRoute(['product/view', 'id' => 42]);
你可以指定一個字符串來作為路由,如:?site/index
?。如果想要指定將要被創(chuàng)建的 URL 的附加查詢參數(shù), 你同樣可以使用一個數(shù)組來作為路由。數(shù)組的格式須為:
// generates: /index.php?r=site/index¶m1=value1¶m2=value2
['site/index', 'param1' => 'value1', 'param2' => 'value2']
如果你想要創(chuàng)建一個帶有 anchor 的 URL ,你可以使用一個帶有?#
?參數(shù)的數(shù)組。比如:
// generates: /index.php?r=site/index¶m1=value1#name
['site/index', 'param1' => 'value1', '#' => 'name']
一個路由既可能是絕對的又可能是相對的。一個絕對的路由以前導(dǎo)斜杠開頭(如:?/site/index
), 而一個相對的路由則沒有(比如:?site/index
?或者?index
)。一個相對的路由將會按照如下規(guī)則轉(zhuǎn)換為絕對路由:
index
?),它會被認為是當(dāng)前控制器的一個 action ID, 然后將會把 \yii\web\Controller::uniqueId 插入到路由前面。site/index
?),它會被認為是相對當(dāng)前模塊(module)的路由, 然后將會把 \yii\base\Module::uniqueId 插入到路由前面。從2.0.2版本開始,你可以用?alias?來指定一個路由。 在這種情況下, alias 將會首先轉(zhuǎn)換為實際的路由, 然后會按照上述規(guī)則轉(zhuǎn)換為絕對路由。
以下是該方法的一些例子:
// /index.php?r=site/index
echo Url::toRoute('site/index');
// /index.php?r=site/index&src=ref1#name
echo 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/index
echo Url::toRoute('site/index', true);
// https://www.example.com/index.php?r=site/index
echo Url::toRoute('site/index', 'https');
還有另外一個方法?Url::to()
?和 toRoute() 非常類似。這兩個方法的唯一區(qū)別在于,前者要求一個路由必須用數(shù)組來指定。 如果傳的參數(shù)為字符串,它將會被直接當(dāng)做 URL 。
Url::to()
?的第一個參數(shù)可以是:
['site/index']
,?['post/index', 'page' => 2]
?。 詳細用法請參考 toRoute() 。@
?的字符串:它將會被當(dāng)做別名, 對應(yīng)的別名字符串將會返回。當(dāng)?$scheme
?指定了(無論是字符串還是 true ),一個帶主機信息(通過 \yii\web\UrlManager::hostInfo 獲得) 的絕對 URL 將會被返回。如果?$url
?已經(jīng)是絕對 URL 了, 它的協(xié)議信息將會被替換為指定的( https 或者 http )。
以下是一些使用示例:
// /index.php?r=site/index
echo Url::to(['site/index']);
// /index.php?r=site/index&src=ref1#name
echo 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 URL
echo Url::to();
// /images/logo.gif
echo Url::to('@web/images/logo.gif');
// images/logo.gif
echo Url::to('images/logo.gif');
// http://www.example.com/images/logo.gif
echo Url::to('@web/images/logo.gif', true);
// https://www.example.com/images/logo.gif
echo Url::to('@web/images/logo.gif', 'https');
從2.0.3版本開始,你可以使用 yii\helpers\Url::current() 來創(chuàng)建一個基于當(dāng)前請求路由和 GET 參數(shù)的 URL。 你可以通過傳遞一個$params
?給這個方法來添加或者刪除 GET 參數(shù)。 例如:
// assume $_GET = ['id' => 123, 'src' => 'google'], current route is "post/view"
// /index.php?r=post/view&id=123&src=google
echo Url::current();
// /index.php?r=post/view&id=123
echo Url::current(['src' => null]);
// /index.php?r=post/view&id=100&src=google
echo Url::current(['id' => 100]);
有時,你需要記住一個 URL 并在后續(xù)的請求處理中使用它。 你可以用以下方式達到這個目的:
// 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ù)的請求處理中,可以用如下方式獲得記住的 URL:
$url = Url::previous();
$productUrl = Url::previous('product');
你可以用如下代碼檢測一個 URL 是否是相對的(比如,包含主機信息部分)。
$isRelative = Url::isRelative('test/it');
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: