Commit 577f8a5c by Alexander Makarov

Added separate schema and absolute parameters

parent 2b842ecc
...@@ -36,11 +36,12 @@ class BaseUrl ...@@ -36,11 +36,12 @@ 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 string $schema URI schema to use. If specified absolute URL with the schema specified is returned. * @param boolean $absolute if absolute URL should be created.
* @param string $schema URI schema to use. Schema to use for absolute URL. If not specified current schema will be used.
* @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 toRoute($route, $schema = null) public static function toRoute($route, $absolute = false, $schema = null)
{ {
$route = (array)$route; $route = (array)$route;
if (!isset($route[0])) { if (!isset($route[0])) {
...@@ -49,7 +50,7 @@ class BaseUrl ...@@ -49,7 +50,7 @@ class BaseUrl
if (Yii::$app->controller instanceof \yii\web\Controller) { if (Yii::$app->controller instanceof \yii\web\Controller) {
$route[0] = static::getNormalizedRoute($route[0]); $route[0] = static::getNormalizedRoute($route[0]);
} }
return $schema === null ? Yii::$app->getUrlManager()->createUrl($route) : Yii::$app->getUrlManager()->createAbsoluteUrl($route, $schema); return $absolute ? Yii::$app->getUrlManager()->createAbsoluteUrl($route, $schema) : Yii::$app->getUrlManager()->createUrl($route);
} }
/** /**
...@@ -95,46 +96,50 @@ class BaseUrl ...@@ -95,46 +96,50 @@ class BaseUrl
* *
* @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 string $schema URI schema to use. If specified absolute URL with the schema specified is returned. * @param boolean $absolute if absolute URL should be created.
* @param string $schema URI schema to use. Schema to use for absolute URL. If not specified current schema will be used.
* @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 = null) public static function to($url = '', $absolute = false, $schema = null)
{ {
if (is_array($url)) { if (is_array($url)) {
return static::toRoute($url, $schema); return static::toRoute($url, $absolute, $schema);
} elseif ($url === '') { } elseif ($url === '') {
if ($schema === null) { if ($absolute) {
return Yii::$app->request->getUrl();
} else {
$url = Yii::$app->request->getAbsoluteUrl(); $url = Yii::$app->request->getAbsoluteUrl();
$pos = strpos($url, '://'); $pos = strpos($url, '://');
return $schema . substr($url, $pos); $url = $schema . substr($url, $pos);
} else {
$url = Yii::$app->request->getUrl();
} }
} else { } else {
$url = Yii::getAlias($url); $url = Yii::getAlias($url);
$pos = strpos($url, '://'); $pos = strpos($url, '://');
if ($pos !== null) { if ($pos !== null) {
// URI is already absolute, adjust schema if specified
if ($schema !== null) { if ($schema !== null) {
$url = $schema . substr($url, $pos); $url = $schema . substr($url, $pos);
} }
return $url;
}
if ($url !== '' && ($url[0] === '/' || $url[0] === '#' || !strncmp($url, './', 2))) {
$url = Yii::$app->getRequest()->getHostInfo() . $url;
} else { } else {
// URI is relative
if ($url === '' || ($url[0] !== '/' && $url[0] !== '#' && strncmp($url, './', 2))) {
// URL is relative need to adjust it to be absolute
$url = Yii::$app->getRequest()->getBaseUrl() . '/' . $url; $url = Yii::$app->getRequest()->getBaseUrl() . '/' . $url;
} }
if ($absolute) {
$url = Yii::$app->getRequest()->getHostInfo() . $url;
if ($schema !== null) { if ($schema !== null) {
$pos = strpos($url, '://'); $pos = strpos($url, '://');
if ($pos !== null) { if ($pos !== null) {
$url = $schema . substr($url, $pos); $url = $schema . substr($url, $pos);
} }
} }
return $url;
} }
} }
}
return $url;
}
/** /**
* Remembers URL passed * Remembers URL passed
...@@ -192,18 +197,22 @@ class BaseUrl ...@@ -192,18 +197,22 @@ class BaseUrl
/** /**
* Returns home URL * Returns home URL
* *
* @param string $schema URI schema to use. If specified absolute URL with the schema specified is returned. * @param boolean $absolute if absolute URL should be created.
* @param string $schema URI schema to use. Schema to use for absolute URL. If not specified current schema will be used.
* @return string home URL * @return string home URL
*/ */
public static function home($schema = null) public static function home($absolute = false, $schema = null)
{ {
if ($schema === null) { if ($absolute) {
return Yii::$app->getHomeUrl();
} else {
$url = Yii::$app->getRequest()->getHostInfo() . Yii::$app->getHomeUrl(); $url = Yii::$app->getRequest()->getHostInfo() . Yii::$app->getHomeUrl();
if ($schema !== null) {
$pos = strpos($url, '://'); $pos = strpos($url, '://');
return $schema . substr($url, $pos); $url = $schema . substr($url, $pos);
}
} else {
$url = Yii::$app->getHomeUrl();
} }
return $url;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment