Commit 5d6c314d by Alexander Makarov

Adjusted URL helper, fixed usage

parent 92b38179
......@@ -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::toRoute(ArrayHelper::getValue($item, 'url', '#'));
$url = Url::to(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::toRoute($url);
return Url::to($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::toRoute(['index']) ?>">
<a href="<?= Url::to(['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::toRoute(['index']) ?>">
<a href="<?= Url::to(['index']) ?>">
<img width="29" height="30" alt="" src="<?= \yii\debug\Module::getYiiLogo() ?>">
Yii Debugger
</a>
......
......@@ -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
$('#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::toRoute($item['url']);
$url = Url::to($item['url']);
} else {
if (!isset($item['content'])) {
throw new InvalidConfigException("The 'content' or 'url' option is required.");
......
......@@ -13,6 +13,7 @@ use Yii;
use Smarty;
use yii\base\View;
use yii\base\ViewRenderer as BaseViewRenderer;
use yii\helpers\Url;
/**
* SmartyViewRenderer allows you to use Smarty templates in views.
......@@ -69,7 +70,7 @@ class ViewRenderer extends BaseViewRenderer
array_unshift($params, $params['route']) ;
unset($params['route']);
return BaseViewRenderer::url($params);
return Url::to($params);
}
/**
......
......@@ -12,6 +12,7 @@ namespace yii\twig;
use Yii;
use yii\base\View;
use yii\base\ViewRenderer as BaseViewRenderer;
use yii\helpers\Url;
/**
* TwigViewRenderer allows you to use Twig templates in views.
......@@ -121,7 +122,7 @@ class ViewRenderer extends BaseViewRenderer
}));
$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);
......
......@@ -107,7 +107,7 @@ class Captcha extends InputWidget
protected function getClientOptions()
{
$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}",
];
return $options;
......
......@@ -219,7 +219,7 @@ class GridView extends BaseListView
}
return [
'filterUrl' => Url::toRoute($filterUrl),
'filterUrl' => Url::to($filterUrl),
'filterSelector' => $filterSelector,
];
}
......
......@@ -205,7 +205,7 @@ class BaseHtml
if (!isset($options['rel'])) {
$options['rel'] = 'stylesheet';
}
$options['href'] = Url::toRoute($url);
$options['href'] = Url::to($url);
return static::tag('link', '', $options);
}
......@@ -221,7 +221,7 @@ class BaseHtml
*/
public static function jsFile($url, $options = [])
{
$options['src'] = Url::toRoute($url);
$options['src'] = Url::to($url);
return static::tag('script', '', $options);
}
......@@ -241,7 +241,7 @@ class BaseHtml
*/
public static function beginForm($action = '', $method = 'post', $options = [])
{
$action = Url::toRoute($action);
$action = Url::to($action);
$hiddenInputs = [];
......@@ -311,7 +311,7 @@ class BaseHtml
public static function a($text, $url = null, $options = [])
{
if ($url !== null) {
$options['href'] = Url::toRoute($url);
$options['href'] = Url::to($url);
}
return static::tag('a', $text, $options);
}
......@@ -346,7 +346,7 @@ class BaseHtml
*/
public static function img($src, $options = [])
{
$options['src'] = Url::toRoute($src);
$options['src'] = Url::to($src);
if (!isset($options['alt'])) {
$options['alt'] = '';
}
......
......@@ -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
* returned without any change further; Otherwise, the result will be prefixed with [[\yii\web\Request::baseUrl]] and returned.
* 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.
*
* @param array|string $url the parameter to be used to generate a valid URL
* @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 ($route !== '' && ($route[0] === '/' || $route[0] === '#' || strpos($route, '://') || !strncmp($route, './', 2))) {
return $route;
if (is_array($url) && isset($url[0]) || $url === '') {
return static::to($url, $absolute);
} else {
$url = Yii::getAlias($url);
if ($url !== '' && ($url[0] === '/' || $url[0] === '#' || strpos($url, '://') || !strncmp($url, './', 2))) {
return $url;
} else {
$result = $absolute ? Yii::$app->getRequest()->getHostInfo() : '';
$result .= Yii::$app->getRequest()->getBaseUrl();
if ($asset !== null) {
$result .= '/' . $asset;
$prefix = $absolute ? Yii::$app->request->getHostInfo() : '';
return $prefix . Yii::$app->getRequest()->getBaseUrl() . '/' . $url;
}
return $result;
}
}
......@@ -125,8 +136,8 @@ class BaseUrl
*/
public function home($absolute = false)
{
$result = $absolute ? Yii::$app->request->getHostInfo() : '';
return $result . Yii::$app->getHomeUrl();
$prefix = $absolute ? Yii::$app->request->getHostInfo() : '';
return $prefix . 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::toRoute($url), $statusCode);
return Yii::$app->getResponse()->redirect(Url::to($url), $statusCode);
}
/**
......
......@@ -673,7 +673,7 @@ class Response extends \yii\base\Response
// ensure the route is absolute
$url[0] = '/' . ltrim($url[0], '/');
}
$url = Url::toRoute($url);
$url = Url::to($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::toRoute($this->validationUrl);
$options['validationUrl'] = Url::to($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::toRoute($item['url']),
'{url}' => Url::to($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