A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.yiiframework.com/doc/api/2.0/yii-helpers-url below:

Url, yii\helpers\Url | API Documentation for Yii 2.0

Class yii\helpers\Url

Url provides a set of static methods for managing URLs.

For more details and usage information on Url, see the guide article on url helpers.

Method Details

Hide inherited methods

public static string base ( $scheme false ) $scheme boolean|string

The URI scheme to use in the returned base URL:

Source code

                public static function base($scheme = false)
{
    $url = static::getUrlManager()->getBaseUrl();
    if ($scheme !== false) {
        $url = static::getUrlManager()->getHostInfo() . $url;
        $url = static::ensureScheme($url, $scheme);
    }
    return $url;
}

            

Source code

                public static function canonical()
{
    $params = Yii::$app->controller->actionParams;
    $params[0] = Yii::$app->controller->getRoute();
    return static::getUrlManager()->createAbsoluteUrl($params);
}

            

Defined in: yii\helpers\BaseUrl::current()

Creates a URL by using the current route and the GET parameters.

You may modify or remove some of the GET parameters, or add additional query parameters through the $params parameter. In particular, if you specify a parameter to be null, then this parameter will be removed from the existing GET parameters; all other parameters specified in $params will be merged with the existing GET parameters. For example,




echo Url::current();


echo Url::current(['src' => null]);


echo Url::current(['id' => 100]);

Note that if you're replacing array parameters with [] at the end you should specify $params as nested arrays. For a PostSearchForm model where parameter names are PostSearchForm[id] and PostSearchForm[src] the syntax would be the following:


echo Url::current([
    $postSearch->formName() => ['id' => 100, 'src' => 'google'],
]);
public static string current ( array $params = [], $scheme false ) $params array

An associative array of parameters that will be merged with the current GET parameters. If a parameter value is null, the corresponding GET parameter will be removed.

$scheme boolean|string

The URI scheme to use in the generated URL:

return string

The generated URL

Source code

                public static function current(array $params = [], $scheme = false)
{
    $currentParams = Yii::$app->getRequest()->getQueryParams();
    $currentParams[0] = '/' . Yii::$app->controller->getRoute();
    $route = array_replace_recursive($currentParams, $params);
    return static::toRoute($route, $scheme);
}

            

Defined in: yii\helpers\BaseUrl::ensureScheme()

Normalize the URL by ensuring it uses specified scheme.

If the URL is relative or the scheme is not a string, normalization is skipped.

public static string ensureScheme ( $url, $scheme ) $url string

The URL to process

$scheme string

The URI scheme used in the URL (e.g. http or https). Use an empty string to create protocol-relative URL (e.g. //example.com/path)

return string

The processed URL

Source code

                public static function ensureScheme($url, $scheme)
{
    if (static::isRelative($url) || !is_string($scheme)) {
        return $url;
    }
    if (strncmp($url, '//', 2) === 0) {
        
        return $scheme === '' ? $url : "$scheme:$url";
    }
    if (($pos = strpos($url, '://')) !== false) {
        if ($scheme === '') {
            $url = substr($url, $pos + 1);
        } else {
            $url = $scheme . substr($url, $pos);
        }
    }
    return $url;
}

            

Source code

                protected static function getUrlManager()
{
    return static::$urlManager ?: Yii::$app->getUrlManager();
}

            
public static string home ( $scheme false ) $scheme boolean|string

The URI scheme to use for the returned URL:

return string

Home URL

Source code

                public static function home($scheme = false)
{
    $url = Yii::$app->getHomeUrl();
    if ($scheme !== false) {
        $url = static::getUrlManager()->getHostInfo() . $url;
        $url = static::ensureScheme($url, $scheme);
    }
    return $url;
}

            

Source code

                public static function isRelative($url)
{
    return preg_match('~^[[:alpha:]][[:alnum:]+-.]*://|^//~', $url) === 0;
}

            

Defined in: yii\helpers\BaseUrl::normalizeRoute()

Normalizes route and makes it suitable for UrlManager. Absolute routes are staying as is while relative routes are converted to absolute ones.

A relative route is a route without a leading slash, such as "view", "post/view".

Starting from version 2.0.2, a route can also be specified as an alias. In this case, the alias will be converted into the actual route first before conducting the above transformation steps.

Source code

                protected static function normalizeRoute($route)
{
    $route = Yii::getAlias((string) $route);
    if (strncmp($route, '/', 1) === 0) {
        
        return ltrim($route, '/');
    }
    
    if (Yii::$app->controller === null) {
        throw new InvalidArgumentException("Unable to resolve the relative route: $route. No active controller is available.");
    }
    if (strpos($route, '/') === false) {
        
        return $route === '' ? Yii::$app->controller->getRoute() : Yii::$app->controller->getUniqueId() . '/' . $route;
    }
    
    return ltrim(Yii::$app->controller->module->getUniqueId() . '/' . $route, '/');
}

            
public static string|null previous ( $name null ) $name string|null

The named associated with the URL that was remembered previously. If not set, yii\web\User::getReturnUrl() will be used to obtain remembered URL.

return string|null

The URL previously remembered. Null is returned if no URL was remembered with the given name and $name is not specified.

Source code

                public static function previous($name = null)
{
    if ($name === null) {
        return Yii::$app->getUser()->getReturnUrl();
    }
    return Yii::$app->getSession()->get($name);
}

            
public static void remember ( $url '', $name null ) $url string|array

The URL to remember. Please refer to to() for acceptable formats. If this parameter is not specified, the currently requested URL will be used.

$name string|null

The name associated with the URL to be remembered. This can be used later by previous(). If not set, yii\web\User::setReturnUrl() will be used with passed URL.

Source code

                public static function remember($url = '', $name = null)
{
    $url = static::to($url);
    if ($name === null) {
        Yii::$app->getUser()->setReturnUrl($url);
    } else {
        Yii::$app->getSession()->set($name, $url);
    }
}

            

Defined in: yii\helpers\BaseUrl::to()

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

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:


echo Url::to(['site/index']);


echo Url::to(['site/index', 'src' => 'ref1', '#' => 'name']);


echo Url::to(['@posts']);


echo Url::to();


echo Url::to('@web/images/logo.gif');


echo Url::to('images/logo.gif');


echo Url::to('@web/images/logo.gif', true);


echo Url::to('@web/images/logo.gif', 'https');


echo Url::to('@web/images/logo.gif', '');
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:

return string

The generated URL

throws yii\base\InvalidArgumentException

a relative route is given while there is no active controller

Source code

                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 (static::isRelative($url)) {
        
        $url = static::getUrlManager()->getHostInfo() . '/' . ltrim($url, '/');
    }
    return static::ensureScheme($url, $scheme);
}

            

Defined in: yii\helpers\BaseUrl::toRoute()

Creates a URL for the given route.

This method will use yii\web\UrlManager to create a URL.

You may specify the route as a string, e.g., site/index. You may also use an array if you want to specify additional query parameters for the URL being created. The array format must be:


['site/index', 'param1' => 'value1', 'param2' => 'value2']

If you want to create a URL with an anchor, you can use the array format with a # parameter. For example,


['site/index', 'param1' => 'value1', '#' => 'name']

A route may be either absolute or relative. An absolute route has a leading slash (e.g. /site/index), while a relative route has none (e.g. site/index or index). A relative route will be converted into an absolute one by the following rules:

Starting from version 2.0.2, a route can also be specified as an alias. In this case, the alias will be converted into the actual route first before conducting the above transformation steps.

Below are some examples of using this method:


echo Url::toRoute('site/index');


echo Url::toRoute(['site/index', 'src' => 'ref1', '#' => 'name']);


echo Url::toRoute('site/index', true);


echo Url::toRoute('site/index', 'https');


echo Url::toRoute('@posts');
public static string toRoute ( $route, $scheme false ) $route string|array

Use a string to represent a route (e.g. index, site/index), or an array to represent a route with query parameters (e.g. ['site/index', 'param1' => 'value1']).

$scheme boolean|string

The URI scheme to use in the generated URL:

return string

The generated URL

throws yii\base\InvalidArgumentException

a relative route is given while there is no active controller

Source code

                public static function toRoute($route, $scheme = false)
{
    $route = (array) $route;
    $route[0] = static::normalizeRoute($route[0]);
    if ($scheme !== false) {
        return static::getUrlManager()->createAbsoluteUrl($route, is_string($scheme) ? $scheme : null);
    }
    return static::getUrlManager()->createUrl($route);
}

            

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4