This method is very similar to
BaseUrl::toRoute. The only difference is that this method
requires a route to be specified as an array only. If a string is given, it will be treated as a URL.
In particular, if
$url is
- an array:
BaseUrl::toRoute will be called to generate the URL. For example:
['site/index'],
['post/index', 'page' => 2]. Please refer to
BaseUrl::toRoute for more details
on how to specify a route.
- a string with a leading
@: it is treated as an alias, and the corresponding aliased string
will be returned.
- an empty string: the currently requested URL will be returned;
- a normal string: it will be returned as is.
When
$scheme is specified (either a string or
true), an absolute URL with host info (obtained from
[[\yii\web\UrlManager::$hostInfo]]) will be returned. If
$url is already an absolute URL, its scheme
will be replaced with the specified one.
Below are some examples of using this method:
php
/index.php?r=site%2Findex
echo Url::to(['site/index']);
/index.php?r=site%2Findex&src=ref1#name
echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name']);
/index.php?r=post%2Findex assume the alias "@posts" is defined as "/post/index"
echo Url::to(['@posts']);
the currently requested URL
echo Url::to();
/images/logo.gif
echo Url::to('@web/images/logo.gif');
images/logo.gif
echo Url::to('images/logo.gif');
http://www.example.com/images/logo.gif
echo Url::to('@web/images/logo.gif', true);
https://www.example.com/images/logo.gif
echo Url::to('@web/images/logo.gif', 'https');
//www.example.com/images/logo.gif
echo Url::to('@web/images/logo.gif', '');