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. |
Résultat | 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);
}
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); }