類別 yii\helpers\BaseUrl
繼承關係 | yii\helpers\BaseUrl |
---|---|
子類別 | yii\helpers\Url |
自版本起可用 | 2.0 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseUrl.php |
BaseUrl 為 yii\helpers\Url 提供了具體的實作。
請勿使用 BaseUrl。請改用 yii\helpers\Url。
公開方法
方法 | 描述 | 定義於 |
---|---|---|
base() | 傳回當前請求的基礎 URL。 | yii\helpers\BaseUrl |
canonical() | 傳回當前請求頁面的正規 URL。 | yii\helpers\BaseUrl |
current() | 使用當前路由和 GET 參數建立 URL。 | yii\helpers\BaseUrl |
ensureScheme() | 通過確保 URL 使用指定的 scheme 來正規化 URL。 | yii\helpers\BaseUrl |
home() | 傳回首頁 URL。 | yii\helpers\BaseUrl |
isRelative() | 傳回一個值,指示 URL 是否為相對路徑。 | yii\helpers\BaseUrl |
previous() | 傳回先前記住的 URL。 | yii\helpers\BaseUrl |
remember() | 記住指定的 URL,以便稍後可以通過 previous() 擷取回來。 | yii\helpers\BaseUrl |
to() | 根據給定的參數建立 URL。 | yii\helpers\BaseUrl |
toRoute() | 為給定的路由建立 URL。 | yii\helpers\BaseUrl |
受保護方法
方法 | 描述 | 定義於 |
---|---|---|
getUrlManager() | yii\helpers\BaseUrl | |
normalizeRoute() | 正規化路由,使其適用於 UrlManager。絕對路由保持不變,而相對路由則轉換為絕對路由。 | yii\helpers\BaseUrl |
屬性詳細資訊
用於建立 URL 的 URL 管理器
方法詳細資訊
傳回當前請求的基礎 URL。
public static (公有靜態) string (字串) base ( $scheme = false (假) ) | ||
$scheme (協定) | boolean (布林值)|string (字串) |
The URI scheme to use in the returned base URL (在回傳的 base URL 中使用的 URI 協定)
|
public static function base($scheme = false)
{
$url = static::getUrlManager()->getBaseUrl();
if ($scheme !== false) {
$url = static::getUrlManager()->getHostInfo() . $url;
$url = static::ensureScheme($url, $scheme);
}
return $url;
}
傳回當前請求頁面的正規 URL。
The canonical URL is constructed using the current controller's yii\web\Controller::$route and yii\web\Controller::$actionParams. You may use the following code in the layout view to add a link tag about canonical URL (Canonical URL 是使用當前控制器的 yii\web\Controller::$route 和 yii\web\Controller::$actionParams 建構而成。您可以在版面視圖中使用以下程式碼來新增關於 Canonical URL 的連結標籤)
$this->registerLinkTag(['rel' => 'canonical', 'href' => Url::canonical()]);
public static (公有靜態) string (字串) canonical ( ) | ||
return (回傳) | string (字串) |
The canonical URL of the currently requested page (當前請求頁面的 Canonical URL) |
---|
public static function canonical()
{
$params = Yii::$app->controller->actionParams;
$params[0] = Yii::$app->controller->getRoute();
return static::getUrlManager()->createAbsoluteUrl($params);
}
使用當前路由和 GET 參數建立 URL。
You may modify or remove some of the GET parameters, or add additional query parameters through the $params
parameter. In particular, if you specify a parameter to be null, then this parameter will be removed from the existing GET parameters; all other parameters specified in $params
will be merged with the existing GET parameters. For example, (您可以透過 $params
參數修改或移除某些 GET 參數,或新增額外的查詢參數。特別是,如果您將參數指定為 null,則此參數將從現有的 GET 參數中移除;$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]);
Note that if you're replacing array parameters with []
at the end you should specify $params
as nested arrays. For a PostSearchForm
model where parameter names are PostSearchForm[id]
and PostSearchForm[src]
the syntax would be the following (請注意,如果您要用結尾為 []
的陣列參數取代,則應將 $params
指定為巢狀陣列。對於 PostSearchForm
模型,其中參數名稱為 PostSearchForm[id]
和 PostSearchForm[src]
,語法如下:)
// index.php?r=post%2Findex&PostSearchForm%5Bid%5D=100&PostSearchForm%5Bsrc%5D=google
echo Url::current([
$postSearch->formName() => ['id' => 100, 'src' => 'google'],
]);
public static (公有靜態) string (字串) current ( array (陣列) $params = [], $scheme = false (假) ) | ||
$params (參數) | array (陣列) |
An associative array of parameters that will be merged with the current GET parameters. If a parameter value is null, the corresponding GET parameter will be removed. (將與當前 GET 參數合併的關聯陣列參數。如果參數值為 null,則會移除對應的 GET 參數。) |
$scheme (協定) | boolean (布林值)|string (字串) |
The URI scheme to use in the generated URL (在產生的 URL 中使用的 URI 協定)
|
return (回傳) | string (字串) |
The generated URL (產生的 URL) |
---|
public static function current(array $params = [], $scheme = false)
{
$currentParams = Yii::$app->getRequest()->getQueryParams();
$currentParams[0] = '/' . Yii::$app->controller->getRoute();
$route = array_replace_recursive($currentParams, $params);
return static::toRoute($route, $scheme);
}
通過確保 URL 使用指定的 scheme 來正規化 URL。
If the URL is relative or the scheme is not a string, normalization is skipped. (如果 URL 是相對的,或協定不是字串,則會跳過正規化。)
public static (公有靜態) string (字串) ensureScheme ( $url, $scheme ) | ||
$url | string (字串) |
The URL to process (要處理的 URL) |
$scheme (協定) | string (字串) |
The URI scheme used in the URL (e.g. |
return (回傳) | string (字串) |
The processed URL (處理後的 URL) |
---|
public static function ensureScheme($url, $scheme)
{
if (static::isRelative($url) || !is_string($scheme)) {
return $url;
}
if (strncmp($url, '//', 2) === 0) {
// e.g. //example.com/path/to/resource
return $scheme === '' ? $url : "$scheme:$url";
}
if (($pos = strpos($url, '://')) !== false) {
if ($scheme === '') {
$url = substr($url, $pos + 1);
} else {
$url = $scheme . substr($url, $pos);
}
}
return $url;
}
protected static (受保護的靜態) yii\web\UrlManager getUrlManager ( ) | ||
return (回傳) | yii\web\UrlManager |
URL manager used to create URLs (用於建立 URL 的 URL 管理器) |
---|
protected static function getUrlManager()
{
return static::$urlManager ?: Yii::$app->getUrlManager();
}
傳回首頁 URL。
public static (公有靜態) string (字串) home ( $scheme = false (假) ) | ||
$scheme (協定) | boolean (布林值)|string (字串) |
The URI scheme to use for the returned URL (用於回傳 URL 的 URI 協定)
|
return (回傳) | string (字串) |
Home URL (首頁 URL) |
---|
public static function home($scheme = false)
{
$url = Yii::$app->getHomeUrl();
if ($scheme !== false) {
$url = static::getUrlManager()->getHostInfo() . $url;
$url = static::ensureScheme($url, $scheme);
}
return $url;
}
傳回一個值,指示 URL 是否為相對路徑。
A relative URL does not have host info part. (相對 URL 沒有主機資訊部分。)
public static (公有靜態) boolean (布林值) isRelative ( $url ) | ||
$url | string (字串) |
The URL to be checked (要檢查的 URL) |
return (回傳) | boolean (布林值) |
Whether the URL is relative (URL 是否為相對) |
---|
public static function isRelative($url)
{
return preg_match('~^[[:alpha:]][[:alnum:]+-.]*://|^//~', $url) === 0;
}
正規化路由,使其適用於 UrlManager。絕對路由保持不變,而相對路由則轉換為絕對路由。
A relative route is a route without a leading slash, such as "view", "post/view". (相對路由是不帶前導斜線的路由,例如 "view"、"post/view"。)
- If the route is an empty string, the current route (路由) will be used; (如果路由是空字串,將會使用目前的 route (路由);)
- If the route contains no slashes at all, it is considered to be an action ID of the current controller and will be prepended with yii\web\Controller::$uniqueId; (如果路由完全不包含斜線,則會將其視為當前控制器的動作 ID,並在其前面加上 yii\web\Controller::$uniqueId;)
- If the route has no leading slash, it is considered to be a route relative to the current module and will be prepended with the module's uniqueId. (如果路由沒有前導斜線,則會將其視為相對於目前模組的路由,並在其前面加上模組的 uniqueId。)
Starting from version 2.0.2, a route can also be specified as an alias. In this case, the alias will be converted into the actual route first before conducting the above transformation steps. (從版本 2.0.2 開始,路由也可以指定為別名。在這種情況下,別名會先轉換為實際路由,然後再執行上述轉換步驟。)
protected static (受保護的靜態) string (字串) normalizeRoute ( $route ) | ||
$route (路由) | string (字串) |
The route. This can be either an absolute route or a relative route. (路由。可以是絕對路由或相對路由。) |
return (回傳) | string (字串) |
Normalized route suitable for UrlManager (適用於 UrlManager 的正規化路由) |
---|---|---|
throws (拋出) | yii\base\InvalidArgumentException |
a relative route is given while there is no active controller (在沒有作用中控制器的情況下給定相對路由) |
protected static function normalizeRoute($route)
{
$route = Yii::getAlias((string) $route);
if (strncmp($route, '/', 1) === 0) {
// absolute route
return ltrim($route, '/');
}
// relative route
if (Yii::$app->controller === null) {
throw new InvalidArgumentException("Unable to resolve the relative route: $route. No active controller is available.");
}
if (strpos($route, '/') === false) {
// empty or an action ID
return $route === '' ? Yii::$app->controller->getRoute() : Yii::$app->controller->getUniqueId() . '/' . $route;
}
// relative to module
return ltrim(Yii::$app->controller->module->getUniqueId() . '/' . $route, '/');
}
public static (公有靜態) string (字串)|null previous ( $name = null ) | ||
$name (名稱) | string (字串)|null |
The named associated with the URL that was remembered previously. If not set, yii\web\User::getReturnUrl() will be used to obtain remembered URL. (與先前記住的 URL 相關聯的名稱。如果未設定,則會使用 yii\web\User::getReturnUrl() 來取得記住的 URL。) |
return (回傳) | string (字串)|null |
The URL previously remembered. Null is returned if no URL was remembered with the given name and |
---|
public static function previous($name = null)
{
if ($name === null) {
return Yii::$app->getUser()->getReturnUrl();
}
return Yii::$app->getSession()->get($name);
}
public static (公有靜態) void (無回傳值) remember ( $url = '', $name = null ) | ||
$url | string (字串)|array (陣列) |
The URL to remember. Please refer to to() for acceptable formats. If this parameter is not specified, the currently requested URL will be used. (要記住的 URL。請參閱 to() 以取得可接受的格式。如果未指定此參數,將會使用目前請求的 URL。) |
$name (名稱) | string (字串)|null |
The name associated with the URL to be remembered. This can be used later by previous(). If not set, yii\web\User::setReturnUrl() will be used with passed URL. (與要記住的 URL 相關聯的名稱。稍後可由 previous() 使用。如果未設定,則會將 yii\web\User::setReturnUrl() 與傳遞的 URL 一起使用。) |
public static function remember($url = '', $name = null)
{
$url = static::to($url);
if ($name === null) {
Yii::$app->getUser()->setReturnUrl($url);
} else {
Yii::$app->getSession()->set($name, $url);
}
}
根據給定的參數建立 URL。
This method is very similar to toRoute(). The only difference is that this method requires a route to be specified as an array only. If a string is given, it will be treated as a URL. In particular, if $url
is (此方法與 toRoute() 非常相似。唯一的區別在於此方法僅要求將路由指定為陣列。如果給定字串,則會將其視為 URL。特別是,如果 $url
是)
- an array: toRoute() will be called to generate the URL. For example:
['site/index']
,['post/index', 'page' => 2]
. Please refer to toRoute() for more details on how to specify a route. (陣列:將呼叫 toRoute() 以產生 URL。例如:['site/index']
、['post/index', 'page' => 2]
。請參閱 toRoute() 以取得關於如何指定路由的更多詳細資訊。) - a string with a leading
@
: it is treated as an alias, and the corresponding aliased string will be returned. (帶有前導@
的字串:會將其視為別名,並回傳對應的別名字串。) - an empty string: the currently requested URL will be returned; (空字串:將回傳目前請求的 URL;)
- a normal string: it will be returned as is. (一般字串:將按原樣回傳。)
When $scheme
is specified (either a string or true
), an absolute URL with host info (obtained from yii\web\UrlManager::$hostInfo) will be returned. If $url
is already an absolute URL, its scheme will be replaced with the specified one. (當指定 $scheme
(字串或 true
) 時,將回傳帶有主機資訊 (從 yii\web\UrlManager::$hostInfo 取得) 的絕對 URL。如果 $url
已經是絕對 URL,其協定將被替換為指定的協定。)
Below are some examples of using this method (以下是一些使用此方法的範例)
// /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%2Findex assume the alias "@posts" is defined as "/post/index"
echo Url::to(['@posts']);
// 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');
// //www.example.com/images/logo.gif
echo Url::to('@web/images/logo.gif', '');
public static (公有靜態) string (字串) to ( $url = '', $scheme = false (假) ) | ||
$url | array (陣列)|string (字串) |
The parameter to be used to generate a valid URL (用於產生有效 URL 的參數) |
$scheme (協定) | boolean (布林值)|string (字串) |
The URI scheme to use in the generated URL (在產生的 URL 中使用的 URI 協定)
|
return (回傳) | string (字串) |
The generated URL (產生的 URL) |
---|---|---|
throws (拋出) | yii\base\InvalidArgumentException |
a relative route is given while there is no active controller (在沒有作用中控制器的情況下給定相對路由) |
public static function to($url = '', $scheme = false)
{
if (is_array($url)) {
return static::toRoute($url, $scheme);
}
$url = Yii::getAlias($url);
if ($url === '') {
$url = Yii::$app->getRequest()->getUrl();
}
if ($scheme === false) {
return $url;
}
if (static::isRelative($url)) {
// turn relative URL into absolute
$url = static::getUrlManager()->getHostInfo() . '/' . ltrim($url, '/');
}
return static::ensureScheme($url, $scheme);
}
為給定的路由建立 URL。
This method will use yii\web\UrlManager to create a URL. (此方法將使用 yii\web\UrlManager 來建立 URL。)
You may specify the route as a string, e.g., site/index
. You may also use an array if you want to specify additional query parameters for the URL being created. The array format must be (您可以將路由指定為字串,例如 site/index
。如果您想為正在建立的 URL 指定額外的查詢參數,也可以使用陣列。陣列格式必須是)
// generates: /index.php?r=site/index¶m1=value1¶m2=value2
['site/index', 'param1' => 'value1', 'param2' => 'value2']
If you want to create a URL with an anchor, you can use the array format with a #
parameter. For example, (如果您想建立帶有錨點的 URL,可以使用帶有 #
參數的陣列格式。例如:)
// generates: /index.php?r=site/index¶m1=value1#name
['site/index', 'param1' => 'value1', '#' => 'name']
A route may be either absolute or relative. An absolute route has a leading slash (e.g. /site/index
), while a relative route has none (e.g. site/index
or index
). A relative route will be converted into an absolute one by the following rules (路由可以是絕對的或相對的。絕對路由帶有前導斜線 (例如 /site/index
),而相對路由則沒有 (例如 site/index
或 index
)。相對路由將通過以下規則轉換為絕對路由)
- If the route is an empty string, the current route (路由) will be used; (如果路由是空字串,將會使用目前的 route (路由);)
- If the route contains no slashes at all (e.g.
index
), it is considered to be an action ID of the current controller and will be prepended with yii\web\Controller::$uniqueId; (如果路由完全不包含斜線 (例如index
),則會將其視為當前控制器的動作 ID,並在其前面加上 yii\web\Controller::$uniqueId;) - If the route has no leading slash (e.g.
site/index
), it is considered to be a route relative to the current module and will be prepended with the module's uniqueId. (如果路由沒有前導斜線 (例如site/index
),則會將其視為相對於目前模組的路由,並在其前面加上模組的 uniqueId。)
Starting from version 2.0.2, a route can also be specified as an alias. In this case, the alias will be converted into the actual route first before conducting the above transformation steps. (從版本 2.0.2 開始,路由也可以指定為別名。在這種情況下,別名會先轉換為實際路由,然後再執行上述轉換步驟。)
Below are some examples of using this method (以下是一些使用此方法的範例)
// /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']);
// 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');
// /index.php?r=post%2Findex assume the alias "@posts" is defined as "post/index"
echo Url::toRoute('@posts');
public static (公有靜態) string (字串) toRoute ( $route, $scheme = false (假) ) | ||
$route (路由) | string (字串)|array (陣列) |
Use a string to represent a route (e.g. |
$scheme (協定) | boolean (布林值)|string (字串) |
The URI scheme to use in the generated URL (在產生的 URL 中使用的 URI 協定)
|
return (回傳) | string (字串) |
The generated URL (產生的 URL) |
---|---|---|
throws (拋出) | yii\base\InvalidArgumentException |
a relative route is given while there is no active controller (在沒有作用中控制器的情況下給定相對路由) |
public static function toRoute($route, $scheme = false)
{
$route = (array) $route;
$route[0] = static::normalizeRoute($route[0]);
if ($scheme !== false) {
return static::getUrlManager()->createAbsoluteUrl($route, is_string($scheme) ? $scheme : null);
}
return static::getUrlManager()->createUrl($route);
}
Signup (註冊) or Login (登入) in order to comment. (為了發表評論,請 註冊 或 登入。)