|
|
@ -7,8 +7,9 @@ |
|
|
|
|
|
|
|
|
|
|
|
namespace yii\helpers; |
|
|
|
namespace yii\helpers; |
|
|
|
|
|
|
|
|
|
|
|
use yii\base\InvalidParamException; |
|
|
|
|
|
|
|
use Yii; |
|
|
|
use Yii; |
|
|
|
|
|
|
|
use yii\base\InvalidParamException; |
|
|
|
|
|
|
|
use yii\web\Controller; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* BaseUrl provides concrete implementation for [[Url]]. |
|
|
|
* BaseUrl provides concrete implementation for [[Url]]. |
|
|
@ -26,9 +27,9 @@ class BaseUrl |
|
|
|
* @param array|string $route route as a string or route and parameters in form of |
|
|
|
* @param array|string $route route as a string or route and parameters in form of |
|
|
|
* `['route', 'param1' => 'value1', 'param2' => 'value2']`. |
|
|
|
* `['route', 'param1' => 'value1', 'param2' => 'value2']`. |
|
|
|
* |
|
|
|
* |
|
|
|
* If there is a controller running relative routes are recognized: |
|
|
|
* If there is a controller running, relative routes are recognized: |
|
|
|
* |
|
|
|
* |
|
|
|
* - If the route is an empty string, the current [[\yii\web\Controller::route]] will be used; |
|
|
|
* - If the route is an empty string, the current [[\yii\web\Controller::route|route]] will be used; |
|
|
|
* - If the route contains no slashes at all, it is considered to be an action ID |
|
|
|
* - 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]]; |
|
|
|
* of the current controller and will be prepended with [[\yii\web\Controller::uniqueId]]; |
|
|
|
* - If the route has no leading slash, it is considered to be a route relative |
|
|
|
* - If the route has no leading slash, it is considered to be a route relative |
|
|
@ -36,26 +37,26 @@ class BaseUrl |
|
|
|
* |
|
|
|
* |
|
|
|
* In case there is no controller, [[\yii\web\UrlManager::createUrl()]] will be used. |
|
|
|
* In case there is no controller, [[\yii\web\UrlManager::createUrl()]] will be used. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param boolean|string $schema URI schema to use: |
|
|
|
* @param boolean|string $scheme URI scheme to use: |
|
|
|
* |
|
|
|
* |
|
|
|
* - `false`: relative URL. Default behavior. |
|
|
|
* - `false`: relative URL. Default behavior. |
|
|
|
* - `true`: absolute URL with the current scheme. |
|
|
|
* - `true`: absolute URL with the current scheme. |
|
|
|
* - string: absolute URL with string value used as schema. |
|
|
|
* - string: absolute URL with string value used as scheme. |
|
|
|
* |
|
|
|
* |
|
|
|
* @return string the normalized URL |
|
|
|
* @return string the URL for the route |
|
|
|
* @throws InvalidParamException if the parameter is invalid. |
|
|
|
* @throws InvalidParamException if the parameter is invalid. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function toRoute($route, $schema = false) |
|
|
|
public static function toRoute($route, $scheme = false) |
|
|
|
{ |
|
|
|
{ |
|
|
|
$route = (array)$route; |
|
|
|
$route = (array)$route; |
|
|
|
if (Yii::$app->controller instanceof \yii\web\Controller) { |
|
|
|
if (Yii::$app->controller instanceof Controller) { |
|
|
|
$route[0] = static::getNormalizedRoute($route[0]); |
|
|
|
$route[0] = static::getNormalizedRoute($route[0]); |
|
|
|
} |
|
|
|
} |
|
|
|
if ($schema) { |
|
|
|
if ($scheme) { |
|
|
|
if ($schema === true) { |
|
|
|
if ($scheme === true) { |
|
|
|
$schema = null; |
|
|
|
$scheme = null; |
|
|
|
} |
|
|
|
} |
|
|
|
$url = Yii::$app->getUrlManager()->createAbsoluteUrl($route, $schema); |
|
|
|
$url = Yii::$app->getUrlManager()->createAbsoluteUrl($route, $scheme); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$url = Yii::$app->getUrlManager()->createUrl($route); |
|
|
|
$url = Yii::$app->getUrlManager()->createUrl($route); |
|
|
|
} |
|
|
|
} |
|
|
@ -63,12 +64,12 @@ class BaseUrl |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Normalizes route making it suitable for UrlManager. Absolute routes are staying as is |
|
|
|
* Normalizes route and makes it suitable for UrlManager. Absolute routes are staying as is |
|
|
|
* while relative routes are converted to absolute routes. |
|
|
|
* while relative routes are converted to absolute ones. |
|
|
|
* |
|
|
|
* |
|
|
|
* A relative route is a route without a leading slash, such as "view", "post/view". |
|
|
|
* A relative route is a route without a leading slash, such as "view", "post/view". |
|
|
|
* |
|
|
|
* |
|
|
|
* - If the route is an empty string, the current [[\yii\web\Controller::route]] will be used; |
|
|
|
* - If the route is an empty string, the current [[\yii\web\Controller::route|route]] will be used; |
|
|
|
* - If the route contains no slashes at all, it is considered to be an action ID |
|
|
|
* - 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]]; |
|
|
|
* of the current controller and will be prepended with [[\yii\web\Controller::uniqueId]]; |
|
|
|
* - If the route has no leading slash, it is considered to be a route relative |
|
|
|
* - If the route has no leading slash, it is considered to be a route relative |
|
|
@ -90,36 +91,36 @@ class BaseUrl |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Creates a link specified by the input parameter. |
|
|
|
* Creates a URL specified by the input parameter. |
|
|
|
* |
|
|
|
* |
|
|
|
* If the input parameter |
|
|
|
* If the input parameter is |
|
|
|
* |
|
|
|
* |
|
|
|
* - is an array: the first array element is considered a route, while the rest of the name-value |
|
|
|
* - an array: the first array element is considered a route, while the rest of the name-value |
|
|
|
* pairs are treated as the parameters to be used for URL creation using [[toRoute]]. |
|
|
|
* pairs are treated as the parameters to be used for URL creation using [[toRoute()]]. |
|
|
|
* For example: `['post/index', 'page' => 2]`, `['index']`. |
|
|
|
* For example: `['post/index', 'page' => 2]`, `['index']`. |
|
|
|
* In case there is no controller, [[\yii\web\UrlManager::createUrl()]] will be used. |
|
|
|
* In case there is no controller, [[\yii\web\UrlManager::createUrl()]] will be used. |
|
|
|
* - is an empty string: the currently requested URL will be returned; |
|
|
|
* - an empty string: the currently requested URL will be returned; |
|
|
|
* - is a non-empty string: it will first be processed by [[Yii::getAlias()]]. If the result |
|
|
|
* - a non-empty string: it will first be processed by [[Yii::getAlias()]]. If the result |
|
|
|
* is an absolute URL, it will be returned either without any change or, if schema was specified, with schema |
|
|
|
* is an absolute URL, it will be returned either without any change or, if scheme was specified, with scheme |
|
|
|
* replaced; Otherwise, the result will be prefixed with [[\yii\web\Request::baseUrl]] and returned. |
|
|
|
* replaced; Otherwise, the result will be prefixed with [[\yii\web\Request::baseUrl]] and returned. |
|
|
|
|
|
|
|
|
|
|
|
* |
|
|
|
* |
|
|
|
* @param array|string $url the parameter to be used to generate a valid URL |
|
|
|
* @param array|string $url the parameter to be used to generate a valid URL |
|
|
|
* @param boolean|string $schema URI schema to use: |
|
|
|
* @param boolean|string $scheme URI scheme to use: |
|
|
|
* |
|
|
|
* |
|
|
|
* - `false`: relative URL. Default behavior. |
|
|
|
* - `false`: relative URL. Default behavior. |
|
|
|
* - `true`: absolute URL with the current scheme. |
|
|
|
* - `true`: absolute URL with the current scheme. |
|
|
|
* - string: absolute URL with string value used as schema. |
|
|
|
* - string: absolute URL with string value used as scheme. |
|
|
|
* |
|
|
|
* |
|
|
|
* @return string the normalized URL |
|
|
|
* @return string the normalized URL |
|
|
|
* @throws InvalidParamException if the parameter is invalid. |
|
|
|
* @throws InvalidParamException if the parameter is invalid. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function to($url = '', $schema = false) |
|
|
|
public static function to($url = '', $scheme = false) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (is_array($url)) { |
|
|
|
if (is_array($url)) { |
|
|
|
return static::toRoute($url, $schema); |
|
|
|
return static::toRoute($url, $scheme); |
|
|
|
} elseif ($url === '') { |
|
|
|
} elseif ($url === '') { |
|
|
|
if ($schema) { |
|
|
|
if ($scheme) { |
|
|
|
$url = Yii::$app->getRequest()->getAbsoluteUrl(); |
|
|
|
$url = Yii::$app->getRequest()->getAbsoluteUrl(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$url = Yii::$app->getRequest()->getUrl(); |
|
|
|
$url = Yii::$app->getRequest()->getUrl(); |
|
|
@ -130,25 +131,26 @@ class BaseUrl |
|
|
|
if ($url === '' || ($url[0] !== '/' && $url[0] !== '#' && strncmp($url, './', 2))) { |
|
|
|
if ($url === '' || ($url[0] !== '/' && $url[0] !== '#' && strncmp($url, './', 2))) { |
|
|
|
$url = Yii::$app->getRequest()->getBaseUrl() . '/' . $url; |
|
|
|
$url = Yii::$app->getRequest()->getBaseUrl() . '/' . $url; |
|
|
|
} |
|
|
|
} |
|
|
|
if ($schema) { |
|
|
|
if ($scheme) { |
|
|
|
$url = Yii::$app->getRequest()->getHostInfo() . $url; |
|
|
|
$url = Yii::$app->getRequest()->getHostInfo() . $url; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if ($schema && $schema !== true) { |
|
|
|
if ($scheme && $scheme !== true) { |
|
|
|
$pos = strpos($url, '://'); |
|
|
|
$pos = strpos($url, '://'); |
|
|
|
if ($pos !== false) { |
|
|
|
if ($pos !== false) { |
|
|
|
$url = $schema . substr($url, $pos); |
|
|
|
$url = $scheme . substr($url, $pos); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return $url; |
|
|
|
return $url; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Remembers URL passed |
|
|
|
* Remembers the specified URL so that it can be later fetched back. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $url URL to remember. Default is current URL. |
|
|
|
* @param string $url URL to remember. Default is the currently requested URL. |
|
|
|
* @param string $name Name to use to remember URL. Defaults to `yii\web\User::returnUrlParam`. |
|
|
|
* @param string $name Name to use to remember URL. Defaults to [[\yii\web\User::returnUrlParam]]. |
|
|
|
|
|
|
|
* @see previous() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function remember($url = '', $name = null) |
|
|
|
public static function remember($url = '', $name = null) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -164,10 +166,11 @@ class BaseUrl |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns URL previously saved with remember method |
|
|
|
* Returns the URL previously [[remember()|remembered]]. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param string $name Name used to remember URL. Defaults to `yii\web\User::returnUrlParam`. |
|
|
|
* @param string $name Name used to remember URL. Defaults to [[\yii\web\User::returnUrlParam]]. |
|
|
|
* @return string URL |
|
|
|
* @return string URL, or null if no such URL was remembered before. |
|
|
|
|
|
|
|
* @see remember() |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function previous($name = null) |
|
|
|
public static function previous($name = null) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -198,23 +201,23 @@ class BaseUrl |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Returns home URL |
|
|
|
* Returns the home URL. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param boolean|string $schema URI schema to use: |
|
|
|
* @param boolean|string $scheme URI scheme to use: |
|
|
|
* |
|
|
|
* |
|
|
|
* - `false`: relative URL. Default behavior. |
|
|
|
* - `false`: relative URL. Default behavior. |
|
|
|
* - `true`: absolute URL with the current scheme. |
|
|
|
* - `true`: absolute URL with the current scheme. |
|
|
|
* - string: absolute URL with string value used as schema. |
|
|
|
* - string: absolute URL with string value used as scheme. |
|
|
|
* |
|
|
|
* |
|
|
|
* @return string home URL |
|
|
|
* @return string home URL |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public static function home($schema = false) |
|
|
|
public static function home($scheme = false) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if ($schema) { |
|
|
|
if ($scheme) { |
|
|
|
$url = Yii::$app->getRequest()->getHostInfo() . Yii::$app->getHomeUrl(); |
|
|
|
$url = Yii::$app->getRequest()->getHostInfo() . Yii::$app->getHomeUrl(); |
|
|
|
if ($schema !== true) { |
|
|
|
if ($scheme !== true) { |
|
|
|
$pos = strpos($url, '://'); |
|
|
|
$pos = strpos($url, '://'); |
|
|
|
$url = $schema . substr($url, $pos); |
|
|
|
$url = $scheme . substr($url, $pos); |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
$url = Yii::$app->getHomeUrl(); |
|
|
|
$url = Yii::$app->getHomeUrl(); |
|
|
@ -222,4 +225,3 @@ class BaseUrl |
|
|
|
return $url; |
|
|
|
return $url; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|