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', '');
public static to ( array | string $url = '', boolean | string $scheme = false ) : string | ||
$url | array | string | the parameter to be used to generate a valid URL |
$scheme | boolean | string | the URI scheme to use in the generated URL: - `false` (default): generating a relative URL. - `true`: returning an absolute base URL whose scheme is the same as that in [[\yii\web\UrlManager::$hostInfo]]. - string: generating an absolute URL with the specified scheme (either `http`, `https` or empty string for protocol-relative URL). |
return | string | the generated URL |
<?php /** * Created by PhpStorm. * User: RomanS * Date: 14.07.2015 * Time: 16:19 */ use yii\helpers\BaseUrl; ?> <div class="menu"> <a class="button" href="<?php echo BaseUrl::to(['contacts/index']); ?> ">Manager</a> <a class="button" href="<?php echo BaseUrl::to(['contacts/events']); ?> ">Events</a> <a class="button" href="<?php echo BaseUrl::to(['users/logout']); ?> ">Logout</a> </div>
<?php $menuItems = [['label' => 'Home', 'url' => ['/site/index']], ['label' => 'About', 'url' => ['/site/about']], ['label' => 'Contact', 'url' => ['/site/contact']], ['label' => 'Blog', 'url' => ['/blog/index']], ['label' => 'Post', 'url' => ['/post']], ['label' => 'Product', 'url' => ['/demo/products']]]; ?> <ul class="nav"> <li><?php echo Html::a('<span class="icon icon-exit-to-app"></span> สถานที่ท่องเที่ยว', '#'); ?> </li> <li><?php echo Html::a('<span class="icon icon-exit-to-app"></span> วัฒนธรรม-ประเพณี', '#'); ?> </li> <li><?php echo Html::a('<span class="icon icon-exit"></span> Blog', BaseUrl::to(['blog/index'])); ?> </li> </ul> <?php if (!Yii::$app->user->isGuest) { ?> <hr> <ul class="nav"> <li> <a href="ui-button.html">Buttons</a> </li> <li> <a href="#"><span class="icon icon-exit-to-app"></span> เมนู</a> <span class="menu-collapse-toggle collapsed" data-target="#form-elements" data-toggle="collapse"><i class="icon icon-close menu-collapse-toggle-close"></i><i class="icon icon-add menu-collapse-toggle-default"></i></span> <ul class="menu-collapse collapse" id="form-elements">
<a href="<?php echo BaseUrl::to(['contacts/edit', 'id' => $contact['idContact']]); ?> "> <button class="view">view</button> </a> <a class="editLink" href="<?php echo BaseUrl::to(['contacts/edit', 'id' => $contact['idContact']]); ?> "> <button class="edit">edit</button> </a> </td> <td class="lastCol" > <?php $href = BaseUrl::to(['contacts/delete', 'id' => $contact['idContact']]); $hrefPage = BaseUrl::current(['page' => $page, 'orderBy' => $orderBy, 'dir' => $dirOpposite, 'sortChange' => $sortChange]); $onclickDelete = "name='" . $contact['lastName'] . ' ' . $contact['firstName'] . "' " . "href='" . $href . "'" . "hrefPage='" . $hrefPage . "'" . "row='" . $i . "'"; ?> <button class="delete" <?php echo $onclickDelete; ?> >X</button> </td> </tr> <?php $num++; } ?> <tr class="lastRow"> <td class="bottomLeft number"></td>