helpers\BaseUrl to()

to() public static method

Creates a URL based on the given parameters.

This method is very similar to 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: toRoute() will be called to generate the URL. For example: ['site/index'], ['post/index', 'page' => 2]. Please refer to 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:

// /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');
public static string to ( $url = '', $scheme = false )
$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 or https).
return string

The generated URL

throws yii\base\InvalidParamException

a relative route is given while there is no active controller

doc_Yii
2016-10-30 17:05:34
Comments
Leave a Comment

Please login to continue.