Commit 92b38179 by Alexander Makarov

Adjusted Url helper methods

parent 73b7e03e
......@@ -136,7 +136,7 @@ class SideNavWidget extends \yii\bootstrap\Widget
$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
// $options = ArrayHelper::getValue($item, 'options', []);
$items = ArrayHelper::getValue($item, 'items');
$url = Url::create(ArrayHelper::getValue($item, 'url', '#'));
$url = Url::toRoute(ArrayHelper::getValue($item, 'url', '#'));
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
Html::addCssClass($linkOptions, 'list-group-item');
......
......@@ -196,7 +196,7 @@ class Choice extends Widget
$this->autoRender = false;
$url = $this->getBaseAuthUrl();
$url[$this->clientIdGetParamName] = $provider->getId();
return Url::create($url);
return Url::toRoute($url);
}
/**
......
......@@ -28,7 +28,7 @@ $url = $firstPanel->getUrl();
?>
<div id="yii-debug-toolbar" class="yii-debug-toolbar-<?= $position ?>">
<div class="yii-debug-toolbar-block title">
<a href="<?= Url::create(['index']) ?>">
<a href="<?= Url::toRoute(['index']) ?>">
<img width="29" height="30" alt="" src="<?= \yii\debug\Module::getYiiLogo() ?>">
Yii Debugger
</a>
......
......@@ -20,7 +20,7 @@ $this->title = 'Yii Debugger';
<div id="yii-debug-toolbar" class="yii-debug-toolbar-top">
<div class="yii-debug-toolbar-block title">
<a href="<?= Url::create(['index']) ?>">
<a href="<?= Url::toRoute(['index']) ?>">
<img width="29" height="30" alt="" src="<?= \yii\debug\Module::getYiiLogo() ?>">
Yii Debugger
</a>
......
......@@ -84,7 +84,7 @@ EOD;
},
]);
}
$ajaxUrl = Url::create(['elasticsearch-query', 'logId' => $logId, 'tag' => $this->tag]);
$ajaxUrl = Url::toRoute(['elasticsearch-query', 'logId' => $logId, 'tag' => $this->tag]);
\Yii::$app->view->registerJs(<<<JS
$('#elastic-link-$i').on('click', function() {
var result = $('#elastic-result-$i');
......
......@@ -121,7 +121,7 @@ class Tabs extends Widget
throw new InvalidConfigException("The 'label' option is required.");
}
if (isset($item['url'])) {
$url = Url::create($item['url']);
$url = Url::toRoute($item['url']);
} else {
if (!isset($item['content'])) {
throw new InvalidConfigException("The 'content' or 'url' option is required.");
......
......@@ -191,7 +191,7 @@ Yii Framework 2 Change Log
- Chg #2544: Changed `DetailView`'s `name:format:label` to `attribute:format:label` to match `GridView` (samdark)
- Chg #2603: `yii\base\ErrorException` now extends `\ErrorException` (samdark)
- Chg #2629: `Module::controllerPath` is now read only, and all controller classes must be namespaced under `Module::controllerNamespace`. (qiangxue)
- Chg #2630: `yii\heplers\Html::url` moved to new `yii\helpers\Url::create` (samdark)
- Chg #2630: `yii\heplers\Html::url` removed (samdark)
- Chg: Renamed `yii\jui\Widget::clientEventsMap` to `clientEventMap` (qiangxue)
- Chg: Renamed `ActiveRecord::getPopulatedRelations()` to `getRelatedRecords()` (qiangxue)
- Chg: Renamed `attributeName` and `className` to `targetAttribute` and `targetClass` for `UniqueValidator` and `ExistValidator` (qiangxue)
......
......@@ -107,7 +107,7 @@ class Captcha extends InputWidget
protected function getClientOptions()
{
$options = [
'refreshUrl' => Url::create(['/' . $this->captchaAction, CaptchaAction::REFRESH_GET_VAR => 1]),
'refreshUrl' => Url::toRoute(['/' . $this->captchaAction, CaptchaAction::REFRESH_GET_VAR => 1]),
'hashKey' => "yiiCaptcha/{$this->captchaAction}",
];
return $options;
......
......@@ -219,7 +219,7 @@ class GridView extends BaseListView
}
return [
'filterUrl' => Url::create($filterUrl),
'filterUrl' => Url::toRoute($filterUrl),
'filterSelector' => $filterSelector,
];
}
......
......@@ -205,7 +205,7 @@ class BaseHtml
if (!isset($options['rel'])) {
$options['rel'] = 'stylesheet';
}
$options['href'] = Url::create($url);
$options['href'] = Url::toRoute($url);
return static::tag('link', '', $options);
}
......@@ -221,7 +221,7 @@ class BaseHtml
*/
public static function jsFile($url, $options = [])
{
$options['src'] = Url::create($url);
$options['src'] = Url::toRoute($url);
return static::tag('script', '', $options);
}
......@@ -241,7 +241,7 @@ class BaseHtml
*/
public static function beginForm($action = '', $method = 'post', $options = [])
{
$action = Url::create($action);
$action = Url::toRoute($action);
$hiddenInputs = [];
......@@ -311,7 +311,7 @@ class BaseHtml
public static function a($text, $url = null, $options = [])
{
if ($url !== null) {
$options['href'] = Url::create($url);
$options['href'] = Url::toRoute($url);
}
return static::tag('a', $text, $options);
}
......@@ -346,7 +346,7 @@ class BaseHtml
*/
public static function img($src, $options = [])
{
$options['src'] = Url::create($src);
$options['src'] = Url::toRoute($src);
if (!isset($options['alt'])) {
$options['alt'] = '';
}
......
......@@ -21,92 +21,90 @@ use Yii;
class BaseUrl
{
/**
* Normalizes the input parameter to be a valid URL.
* Returns URL for a route.
*
* If the input parameter
* @param array|string $route parametrized route or empty string for current URL:
*
* - is 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
* is an absolute URL, it will be returned without any change further; Otherwise, the result
* will be prefixed with [[\yii\web\Request::baseUrl]] and returned.
* - is an array: the first array element is considered a route, while the rest of the name-value
* - an empty string: the currently requested URL will be returned;
* - 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 [[\yii\web\Controller::createUrl()]].
* For example: `['post/index', 'page' => 2]`, `['index']`.
* In case there is no controller, [[\yii\web\UrlManager::createUrl()]] will be used.
*
* @param array|string $params the parameter to be used to generate a valid URL
* @param boolean $absolute if absolute URL should be created
* @return string the normalized URL
* @throws InvalidParamException if the parameter is invalid.
*/
public static function create($params, $absolute = false)
public static function toRoute($route, $absolute = false)
{
if (is_array($params)) {
if (isset($params[0])) {
if (Yii::$app->controller instanceof \yii\web\Controller) {
return $absolute ? Yii::$app->controller->createAbsoluteUrl($params) : Yii::$app->controller->createUrl($params);
} else {
return $absolute ? Yii::$app->getUrlManager()->createAbsoluteUrl($params) : Yii::$app->getUrlManager()->createUrl($params);
if ($route === '') {
return $absolute ? Yii::$app->getRequest()->getAbsoluteUrl() : Yii::$app->getRequest()->getUrl();
}
} else {
throw new InvalidParamException('The array specifying a URL must contain at least one element.');
if (!is_array($route) || !isset($route[0])) {
throw new InvalidParamException('$route should be either empty string or array containing at least one element.');
}
} elseif ($params === '') {
return $absolute ? Yii::$app->getRequest()->getAbsoluteUrl() : Yii::$app->getRequest()->getUrl();
} else {
$params = Yii::getAlias($params);
if ($params !== '' && ($params[0] === '/' || $params[0] === '#' || strpos($params, '://') || !strncmp($params, './', 2))) {
return $params;
if (Yii::$app->controller instanceof \yii\web\Controller) {
return $absolute ? Yii::$app->controller->createAbsoluteUrl($route) : Yii::$app->controller->createUrl($route);
} else {
return static::base($params);
}
return $absolute ? Yii::$app->getUrlManager()->createAbsoluteUrl($route) : Yii::$app->getUrlManager()->createUrl($route);
}
}
/**
* Normalizes the input parameter to be a valid absolute URL.
*
* If the input parameter
*
* - is 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
* is an absolute URL, it will be returned without any change further; Otherwise, the result
* will be prefixed with [[\yii\web\Request::baseUrl]] and returned.
* - is 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 [[\yii\web\Controller::createUrl()]].
* For example: `['post/index', 'page' => 2]`, `['index']`.
* In case there is no controller, [[\yii\web\UrlManager::createUrl()]] will be used.
* Returns URL to asset located under webroot
*
* @param array|string $params the parameter to be used to generate a valid URL
* @return string the normalized URL
* @throws InvalidParamException if the parameter is invalid.
* @param string $asset will first be processed by [[Yii::getAlias()]]. If the result is an absolute URL, it will be
* returned without any change further; Otherwise, the result will be prefixed with [[\yii\web\Request::baseUrl]] and returned.
* @param boolean $absolute if URL should be absolute
* @return string
*/
public static function createAbsolute($params)
public function toAsset($asset = null, $absolute = false)
{
return static::create($params, true);
$route = Yii::getAlias($asset);
if ($route !== '' && ($route[0] === '/' || $route[0] === '#' || strpos($route, '://') || !strncmp($route, './', 2))) {
return $route;
} else {
$result = $absolute ? Yii::$app->getRequest()->getHostInfo() : '';
$result .= Yii::$app->getRequest()->getBaseUrl();
if ($asset !== null) {
$result .= '/' . $asset;
}
return $result;
}
}
/**
* Prefixes relative URL with base URL
* Remembers URL passed
*
* @param string $url relative URL
* @return string absolute URL
* @param string $url URL to remember. Default is current URL.
* @param string $name Name to use to remember URL. Defaults to `yii\web\User::returnUrlParam`.
*/
public function base($url = null)
public function remember($url = null, $name = null)
{
$result = Yii::$app->getRequest()->getBaseUrl();
if ($url !== null) {
$result .= '/' . $url;
if ($url === null) {
$url = Yii::$app->getRequest()->getUrl();
}
if ($name === null) {
Yii::$app->user->setReturnUrl($url);
} else {
Yii::$app->session->set($name, $url);
}
return $result;
}
/**
* Sets current URL as return URL
* Returns URL previously saved with remember method
*
* @param string $name Name used to remember URL. Defaults to `yii\web\User::returnUrlParam`.
* @return string URL
*/
public function rememberReturn()
public function previous($name = null)
{
Yii::$app->user->setReturnUrl(Yii::$app->getRequest()->getUrl());
if ($name === null) {
return Yii::$app->user->getReturnUrl();
} else {
return Yii::$app->session->get($name);
}
}
/**
......@@ -114,7 +112,7 @@ class BaseUrl
*
* @return string canonical URL
*/
public function getCanonical()
public function canonical()
{
return Yii::$app->controller->getCanonicalUrl();
}
......@@ -122,11 +120,13 @@ class BaseUrl
/**
* Returns home URL
*
* @param boolean $absolute if absolute URL should be returned
* @return string home URL
*/
public function getHome()
public function home($absolute = false)
{
return Yii::$app->getHomeUrl();
$result = $absolute ? Yii::$app->request->getHostInfo() : '';
return $result . Yii::$app->getHomeUrl();
}
}
\ No newline at end of file
......@@ -241,7 +241,7 @@ class Controller extends \yii\base\Controller
*/
public function redirect($url, $statusCode = 302)
{
return Yii::$app->getResponse()->redirect(Url::create($url), $statusCode);
return Yii::$app->getResponse()->redirect(Url::toRoute($url), $statusCode);
}
/**
......
......@@ -673,7 +673,7 @@ class Response extends \yii\base\Response
// ensure the route is absolute
$url[0] = '/' . ltrim($url[0], '/');
}
$url = Url::create($url);
$url = Url::toRoute($url);
if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) {
$url = Yii::$app->getRequest()->getHostInfo() . $url;
}
......
......@@ -195,7 +195,7 @@ class ActiveForm extends Widget
'ajaxDataType' => $this->ajaxDataType,
];
if ($this->validationUrl !== null) {
$options['validationUrl'] = Url::create($this->validationUrl);
$options['validationUrl'] = Url::toRoute($this->validationUrl);
}
foreach (['beforeSubmit', 'beforeValidate', 'afterValidate'] as $name) {
if (($value = $this->$name) !== null) {
......
......@@ -219,7 +219,7 @@ class Menu extends Widget
if (isset($item['url'])) {
$template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
return strtr($template, [
'{url}' => Url::create($item['url']),
'{url}' => Url::toRoute($item['url']),
'{label}' => $item['label'],
]);
} else {
......
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