2 追隨者

Url 助手

Url 助手提供一組靜態方法來管理 URL。

取得常用 URL

您可以使用兩種方法來取得常用 URL:首頁 URL 和目前請求的基礎 URL。為了取得首頁 URL,請使用以下程式碼

$relativeHomeUrl = Url::home();
$absoluteHomeUrl = Url::home(true);
$httpsAbsoluteHomeUrl = Url::home('https');

如果沒有傳遞參數,則產生的 URL 是相對的。您可以傳遞 true 以取得目前結構描述的絕對 URL,或明確指定結構描述 (httpshttp)。

若要取得目前請求的基礎 URL,請使用以下程式碼

$relativeBaseUrl = Url::base();
$absoluteBaseUrl = Url::base(true);
$httpsAbsoluteBaseUrl = Url::base('https');

此方法的唯一參數與 Url::home() 的參數完全相同。

建立 URL

為了建立指向給定路由的 URL,請使用 Url::toRoute() 方法。此方法使用 yii\web\UrlManager 來建立 URL

$url = Url::toRoute(['product/view', 'id' => 42]);

您可以將路由指定為字串,例如 site/index。如果您想要為正在建立的 URL 指定其他查詢參數,也可以使用陣列。陣列格式必須是

// generates: /index.php?r=site%2Findex&param1=value1&param2=value2
['site/index', 'param1' => 'value1', 'param2' => 'value2']

如果您想要建立帶有錨點的 URL,可以使用帶有 # 參數的陣列格式。例如:

// generates: /index.php?r=site%2Findex&param1=value1#name
['site/index', 'param1' => 'value1', '#' => 'name']

路由可以是絕對或相對的。絕對路由具有前導斜線(例如 /site/index),而相對路由則沒有(例如 site/indexindex)。相對路由將透過以下規則轉換為絕對路由

  • 如果路由是空字串,將使用目前的 route
  • 如果路由完全不包含斜線(例如 index),則會將其視為目前控制器的動作 ID,並在前面加上 yii\web\Controller::$uniqueId
  • 如果路由沒有前導斜線(例如 site/index),則會將其視為相對於目前模組的路由,並在前面加上模組的 uniqueId

從 2.0.2 版本開始,您可以根據 別名 指定路由。如果是這種情況,別名將首先轉換為實際路由,然後根據上述規則轉換為絕對路由。

以下是一些使用此方法的範例

// /index.php?r=site%2Findex
echo Url::toRoute('site/index');

// /index.php?r=site%2Findex&src=ref1#name
echo Url::toRoute(['site/index', 'src' => 'ref1', '#' => 'name']);

// /index.php?r=post%2Fedit&id=100     assume the alias "@postEdit" is defined as "post/edit"
echo Url::toRoute(['@postEdit', 'id' => 100]);

// https://www.example.com/index.php?r=site%2Findex
echo Url::toRoute('site/index', true);

// https://www.example.com/index.php?r=site%2Findex
echo Url::toRoute('site/index', 'https');

還有另一種方法 Url::to(),它與 toRoute() 非常相似。唯一的區別是此方法要求路由只能指定為陣列。如果給定字串,它將被視為 URL。

第一個參數可以是

  • 陣列:將呼叫 toRoute() 以產生 URL。例如:['site/index']['post/index', 'page' => 2]。有關如何指定路由的更多詳細資訊,請參閱 toRoute()
  • 帶有前導 @ 的字串:它被視為別名,並傳回對應的別名字串。
  • 空字串:將傳回目前請求的 URL;
  • 一般字串:將按原樣傳回。

當指定 $scheme(字串或 true)時,將傳回帶有主機資訊(從 yii\web\UrlManager::$hostInfo 取得)的絕對 URL。如果 $url 已經是絕對 URL,其結構描述將被替換為指定的結構描述。

以下是一些使用範例

// /index.php?r=site%2Findex
echo Url::to(['site/index']);

// /index.php?r=site%2Findex&src=ref1#name
echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name']);

// /index.php?r=post%2Fedit&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');

// https://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() 根據目前請求的路由和 GET 參數建立 URL。您可以透過將 $params 參數傳遞給方法來修改或移除某些 GET 參數,或新增參數。例如:

// assume $_GET = ['id' => 123, 'src' => 'google'], current route is "post/view"

// /index.php?r=post%2Fview&id=123&src=google
echo Url::current();

// /index.php?r=post%2Fview&id=123
echo Url::current(['src' => null]);
// /index.php?r=post%2Fview&id=100&src=google
echo Url::current(['id' => 100]);

記住 URL

在某些情況下,您需要記住 URL,然後在後續請求的處理過程中使用它。這可以透過以下方式實現

// 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');

在下一個請求中,我們可以透過以下方式取得記住的 URL

$url = Url::previous();
$productUrl = Url::previous('product');

檢查相對 URL

若要找出 URL 是否為相對 URL,即它沒有主機資訊部分,您可以使用以下程式碼

$isRelative = Url::isRelative('test/it');

發現錯字或您認為此頁面需要改進?
在 github 上編輯它 !