Commit 5d6c314d by Alexander Makarov

Adjusted URL helper, fixed usage

parent 92b38179
...@@ -136,7 +136,7 @@ class SideNavWidget extends \yii\bootstrap\Widget ...@@ -136,7 +136,7 @@ class SideNavWidget extends \yii\bootstrap\Widget
$label = $this->encodeLabels ? Html::encode($item['label']) : $item['label']; $label = $this->encodeLabels ? Html::encode($item['label']) : $item['label'];
// $options = ArrayHelper::getValue($item, 'options', []); // $options = ArrayHelper::getValue($item, 'options', []);
$items = ArrayHelper::getValue($item, 'items'); $items = ArrayHelper::getValue($item, 'items');
$url = Url::toRoute(ArrayHelper::getValue($item, 'url', '#')); $url = Url::to(ArrayHelper::getValue($item, 'url', '#'));
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
Html::addCssClass($linkOptions, 'list-group-item'); Html::addCssClass($linkOptions, 'list-group-item');
......
...@@ -196,7 +196,7 @@ class Choice extends Widget ...@@ -196,7 +196,7 @@ class Choice extends Widget
$this->autoRender = false; $this->autoRender = false;
$url = $this->getBaseAuthUrl(); $url = $this->getBaseAuthUrl();
$url[$this->clientIdGetParamName] = $provider->getId(); $url[$this->clientIdGetParamName] = $provider->getId();
return Url::toRoute($url); return Url::to($url);
} }
/** /**
......
...@@ -28,7 +28,7 @@ $url = $firstPanel->getUrl(); ...@@ -28,7 +28,7 @@ $url = $firstPanel->getUrl();
?> ?>
<div id="yii-debug-toolbar" class="yii-debug-toolbar-<?= $position ?>"> <div id="yii-debug-toolbar" class="yii-debug-toolbar-<?= $position ?>">
<div class="yii-debug-toolbar-block title"> <div class="yii-debug-toolbar-block title">
<a href="<?= Url::toRoute(['index']) ?>"> <a href="<?= Url::to(['index']) ?>">
<img width="29" height="30" alt="" src="<?= \yii\debug\Module::getYiiLogo() ?>"> <img width="29" height="30" alt="" src="<?= \yii\debug\Module::getYiiLogo() ?>">
Yii Debugger Yii Debugger
</a> </a>
......
...@@ -20,7 +20,7 @@ $this->title = 'Yii Debugger'; ...@@ -20,7 +20,7 @@ $this->title = 'Yii Debugger';
<div id="yii-debug-toolbar" class="yii-debug-toolbar-top"> <div id="yii-debug-toolbar" class="yii-debug-toolbar-top">
<div class="yii-debug-toolbar-block title"> <div class="yii-debug-toolbar-block title">
<a href="<?= Url::toRoute(['index']) ?>"> <a href="<?= Url::to(['index']) ?>">
<img width="29" height="30" alt="" src="<?= \yii\debug\Module::getYiiLogo() ?>"> <img width="29" height="30" alt="" src="<?= \yii\debug\Module::getYiiLogo() ?>">
Yii Debugger Yii Debugger
</a> </a>
......
...@@ -84,7 +84,7 @@ EOD; ...@@ -84,7 +84,7 @@ EOD;
}, },
]); ]);
} }
$ajaxUrl = Url::toRoute(['elasticsearch-query', 'logId' => $logId, 'tag' => $this->tag]); $ajaxUrl = Url::to(['elasticsearch-query', 'logId' => $logId, 'tag' => $this->tag]);
\Yii::$app->view->registerJs(<<<JS \Yii::$app->view->registerJs(<<<JS
$('#elastic-link-$i').on('click', function() { $('#elastic-link-$i').on('click', function() {
var result = $('#elastic-result-$i'); var result = $('#elastic-result-$i');
......
...@@ -121,7 +121,7 @@ class Tabs extends Widget ...@@ -121,7 +121,7 @@ class Tabs extends Widget
throw new InvalidConfigException("The 'label' option is required."); throw new InvalidConfigException("The 'label' option is required.");
} }
if (isset($item['url'])) { if (isset($item['url'])) {
$url = Url::toRoute($item['url']); $url = Url::to($item['url']);
} else { } else {
if (!isset($item['content'])) { if (!isset($item['content'])) {
throw new InvalidConfigException("The 'content' or 'url' option is required."); throw new InvalidConfigException("The 'content' or 'url' option is required.");
......
...@@ -13,6 +13,7 @@ use Yii; ...@@ -13,6 +13,7 @@ use Yii;
use Smarty; use Smarty;
use yii\base\View; use yii\base\View;
use yii\base\ViewRenderer as BaseViewRenderer; use yii\base\ViewRenderer as BaseViewRenderer;
use yii\helpers\Url;
/** /**
* SmartyViewRenderer allows you to use Smarty templates in views. * SmartyViewRenderer allows you to use Smarty templates in views.
...@@ -69,7 +70,7 @@ class ViewRenderer extends BaseViewRenderer ...@@ -69,7 +70,7 @@ class ViewRenderer extends BaseViewRenderer
array_unshift($params, $params['route']) ; array_unshift($params, $params['route']) ;
unset($params['route']); unset($params['route']);
return BaseViewRenderer::url($params); return Url::to($params);
} }
/** /**
......
...@@ -12,6 +12,7 @@ namespace yii\twig; ...@@ -12,6 +12,7 @@ namespace yii\twig;
use Yii; use Yii;
use yii\base\View; use yii\base\View;
use yii\base\ViewRenderer as BaseViewRenderer; use yii\base\ViewRenderer as BaseViewRenderer;
use yii\helpers\Url;
/** /**
* TwigViewRenderer allows you to use Twig templates in views. * TwigViewRenderer allows you to use Twig templates in views.
...@@ -121,7 +122,7 @@ class ViewRenderer extends BaseViewRenderer ...@@ -121,7 +122,7 @@ class ViewRenderer extends BaseViewRenderer
})); }));
$this->twig->addFunction('path', new \Twig_Function_Function(function ($path, $args = []) { $this->twig->addFunction('path', new \Twig_Function_Function(function ($path, $args = []) {
return BaseViewRenderer::url(array_merge([$path], $args)); return Url::to(array_merge([$path], $args));
})); }));
$this->twig->addGlobal('app', \Yii::$app); $this->twig->addGlobal('app', \Yii::$app);
......
...@@ -107,7 +107,7 @@ class Captcha extends InputWidget ...@@ -107,7 +107,7 @@ class Captcha extends InputWidget
protected function getClientOptions() protected function getClientOptions()
{ {
$options = [ $options = [
'refreshUrl' => Url::toRoute(['/' . $this->captchaAction, CaptchaAction::REFRESH_GET_VAR => 1]), 'refreshUrl' => Url::to(['/' . $this->captchaAction, CaptchaAction::REFRESH_GET_VAR => 1]),
'hashKey' => "yiiCaptcha/{$this->captchaAction}", 'hashKey' => "yiiCaptcha/{$this->captchaAction}",
]; ];
return $options; return $options;
......
...@@ -219,7 +219,7 @@ class GridView extends BaseListView ...@@ -219,7 +219,7 @@ class GridView extends BaseListView
} }
return [ return [
'filterUrl' => Url::toRoute($filterUrl), 'filterUrl' => Url::to($filterUrl),
'filterSelector' => $filterSelector, 'filterSelector' => $filterSelector,
]; ];
} }
......
...@@ -205,7 +205,7 @@ class BaseHtml ...@@ -205,7 +205,7 @@ class BaseHtml
if (!isset($options['rel'])) { if (!isset($options['rel'])) {
$options['rel'] = 'stylesheet'; $options['rel'] = 'stylesheet';
} }
$options['href'] = Url::toRoute($url); $options['href'] = Url::to($url);
return static::tag('link', '', $options); return static::tag('link', '', $options);
} }
...@@ -221,7 +221,7 @@ class BaseHtml ...@@ -221,7 +221,7 @@ class BaseHtml
*/ */
public static function jsFile($url, $options = []) public static function jsFile($url, $options = [])
{ {
$options['src'] = Url::toRoute($url); $options['src'] = Url::to($url);
return static::tag('script', '', $options); return static::tag('script', '', $options);
} }
...@@ -241,7 +241,7 @@ class BaseHtml ...@@ -241,7 +241,7 @@ class BaseHtml
*/ */
public static function beginForm($action = '', $method = 'post', $options = []) public static function beginForm($action = '', $method = 'post', $options = [])
{ {
$action = Url::toRoute($action); $action = Url::to($action);
$hiddenInputs = []; $hiddenInputs = [];
...@@ -311,7 +311,7 @@ class BaseHtml ...@@ -311,7 +311,7 @@ class BaseHtml
public static function a($text, $url = null, $options = []) public static function a($text, $url = null, $options = [])
{ {
if ($url !== null) { if ($url !== null) {
$options['href'] = Url::toRoute($url); $options['href'] = Url::to($url);
} }
return static::tag('a', $text, $options); return static::tag('a', $text, $options);
} }
...@@ -346,7 +346,7 @@ class BaseHtml ...@@ -346,7 +346,7 @@ class BaseHtml
*/ */
public static function img($src, $options = []) public static function img($src, $options = [])
{ {
$options['src'] = Url::toRoute($src); $options['src'] = Url::to($src);
if (!isset($options['alt'])) { if (!isset($options['alt'])) {
$options['alt'] = ''; $options['alt'] = '';
} }
......
...@@ -51,25 +51,36 @@ class BaseUrl ...@@ -51,25 +51,36 @@ class BaseUrl
} }
/** /**
* Returns URL to asset located under webroot * Normalizes the input parameter to be a valid URL.
* *
* @param string $asset will first be processed by [[Yii::getAlias()]]. If the result is an absolute URL, it will be * If the input parameter
* returned without any change further; Otherwise, the result will be prefixed with [[\yii\web\Request::baseUrl]] and returned. *
* - 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.
*
* @param array|string $url the parameter to be used to generate a valid URL
* @param boolean $absolute if URL should be absolute * @param boolean $absolute if URL should be absolute
* @return string * @return string the normalized URL
* @throws InvalidParamException if the parameter is invalid.
*/ */
public function toAsset($asset = null, $absolute = false) public static function to($url = null, $absolute = false)
{ {
$route = Yii::getAlias($asset); if (is_array($url) && isset($url[0]) || $url === '') {
if ($route !== '' && ($route[0] === '/' || $route[0] === '#' || strpos($route, '://') || !strncmp($route, './', 2))) { return static::to($url, $absolute);
return $route;
} else { } else {
$result = $absolute ? Yii::$app->getRequest()->getHostInfo() : ''; $url = Yii::getAlias($url);
$result .= Yii::$app->getRequest()->getBaseUrl(); if ($url !== '' && ($url[0] === '/' || $url[0] === '#' || strpos($url, '://') || !strncmp($url, './', 2))) {
if ($asset !== null) { return $url;
$result .= '/' . $asset; } else {
$prefix = $absolute ? Yii::$app->request->getHostInfo() : '';
return $prefix . Yii::$app->getRequest()->getBaseUrl() . '/' . $url;
} }
return $result;
} }
} }
...@@ -125,8 +136,8 @@ class BaseUrl ...@@ -125,8 +136,8 @@ class BaseUrl
*/ */
public function home($absolute = false) public function home($absolute = false)
{ {
$result = $absolute ? Yii::$app->request->getHostInfo() : ''; $prefix = $absolute ? Yii::$app->request->getHostInfo() : '';
return $result . Yii::$app->getHomeUrl(); return $prefix . Yii::$app->getHomeUrl();
} }
} }
\ No newline at end of file
...@@ -241,7 +241,7 @@ class Controller extends \yii\base\Controller ...@@ -241,7 +241,7 @@ class Controller extends \yii\base\Controller
*/ */
public function redirect($url, $statusCode = 302) public function redirect($url, $statusCode = 302)
{ {
return Yii::$app->getResponse()->redirect(Url::toRoute($url), $statusCode); return Yii::$app->getResponse()->redirect(Url::to($url), $statusCode);
} }
/** /**
......
...@@ -673,7 +673,7 @@ class Response extends \yii\base\Response ...@@ -673,7 +673,7 @@ class Response extends \yii\base\Response
// ensure the route is absolute // ensure the route is absolute
$url[0] = '/' . ltrim($url[0], '/'); $url[0] = '/' . ltrim($url[0], '/');
} }
$url = Url::toRoute($url); $url = Url::to($url);
if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) { if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) {
$url = Yii::$app->getRequest()->getHostInfo() . $url; $url = Yii::$app->getRequest()->getHostInfo() . $url;
} }
......
...@@ -195,7 +195,7 @@ class ActiveForm extends Widget ...@@ -195,7 +195,7 @@ class ActiveForm extends Widget
'ajaxDataType' => $this->ajaxDataType, 'ajaxDataType' => $this->ajaxDataType,
]; ];
if ($this->validationUrl !== null) { if ($this->validationUrl !== null) {
$options['validationUrl'] = Url::toRoute($this->validationUrl); $options['validationUrl'] = Url::to($this->validationUrl);
} }
foreach (['beforeSubmit', 'beforeValidate', 'afterValidate'] as $name) { foreach (['beforeSubmit', 'beforeValidate', 'afterValidate'] as $name) {
if (($value = $this->$name) !== null) { if (($value = $this->$name) !== null) {
......
...@@ -219,7 +219,7 @@ class Menu extends Widget ...@@ -219,7 +219,7 @@ class Menu extends Widget
if (isset($item['url'])) { if (isset($item['url'])) {
$template = ArrayHelper::getValue($item, 'template', $this->linkTemplate); $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);
return strtr($template, [ return strtr($template, [
'{url}' => Url::toRoute($item['url']), '{url}' => Url::to($item['url']),
'{label}' => $item['label'], '{label}' => $item['label'],
]); ]);
} else { } 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