yii\helpers\BaseUrl::to PHP Метод

to() публичный статический Метод

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).
Результат string the generated URL
    public static function to($url = '', $scheme = false)
    {
        if (is_array($url)) {
            return static::toRoute($url, $scheme);
        }
        $url = Yii::getAlias($url);
        if ($url === '') {
            $url = Yii::$app->getRequest()->getUrl();
        }
        if ($scheme === false) {
            return $url;
        }
        if (($pos = strpos($url, ':')) === false || !ctype_alpha(substr($url, 0, $pos))) {
            // turn relative URL into absolute
            $url = static::getUrlManager()->getHostInfo() . '/' . ltrim($url, '/');
        }
        return static::ensureScheme($url, $scheme);
    }

Usage Example

Пример #1
0
<?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>
All Usage Examples Of yii\helpers\BaseUrl::to