yii\web\UrlManager::createAbsoluteUrl PHP Method

createAbsoluteUrl() public method

This method prepends the URL created by UrlManager::createUrl with the [[hostInfo]]. Note that unlike [[\yii\helpers\Url::toRoute()]], this method always treats the given route as an absolute route.
See also: createUrl()
public createAbsoluteUrl ( string | array $params, string | null $scheme = null ) : string
$params string | array use a string to represent a route (e.g. `site/index`), or an array to represent a route with query parameters (e.g. `['site/index', 'param1' => 'value1']`).
$scheme string | null the scheme to use for the URL (either `http`, `https` or empty string for protocol-relative URL). If not specified the scheme of the current request will be used.
return string the created URL
    public function createAbsoluteUrl($params, $scheme = null)
    {
        $params = (array) $params;
        $url = $this->createUrl($params);
        if (strpos($url, '://') === false) {
            $url = $this->getHostInfo() . $url;
        }
        return Url::ensureScheme($url, $scheme);
    }

Usage Example

Example #1
0
 public function testCreateAbsoluteUrl()
 {
     $manager = new UrlManager(['baseUrl' => '/', 'hostInfo' => 'http://www.example.com', 'cache' => null]);
     $url = $manager->createAbsoluteUrl(['post/view', 'id' => 1, 'title' => 'sample post']);
     $this->assertEquals('http://www.example.com?r=post%2Fview&id=1&title=sample+post', $url);
     $url = $manager->createAbsoluteUrl(['post/view', 'id' => 1, 'title' => 'sample post'], 'https');
     $this->assertEquals('https://www.example.com?r=post%2Fview&id=1&title=sample+post', $url);
     $manager->hostInfo = 'https://www.example.com';
     $url = $manager->createAbsoluteUrl(['post/view', 'id' => 1, 'title' => 'sample post'], 'http');
     $this->assertEquals('http://www.example.com?r=post%2Fview&id=1&title=sample+post', $url);
 }
All Usage Examples Of yii\web\UrlManager::createAbsoluteUrl