diff --git a/framework/helpers/BaseUrl.php b/framework/helpers/BaseUrl.php index 69b240456a6..5623aec5128 100644 --- a/framework/helpers/BaseUrl.php +++ b/framework/helpers/BaseUrl.php @@ -223,7 +223,7 @@ public static function to($url = '', $scheme = false) return $url; } - if (($pos = strpos($url, ':')) === false || !ctype_alpha(substr($url, 0, $pos))) { + if (static::isRelative($url)) { // turn relative URL into absolute $url = static::getUrlManager()->getHostInfo() . '/' . ltrim($url, '/'); } diff --git a/tests/framework/helpers/UrlTest.php b/tests/framework/helpers/UrlTest.php index 391d894c737..1be2ffe43e9 100644 --- a/tests/framework/helpers/UrlTest.php +++ b/tests/framework/helpers/UrlTest.php @@ -186,6 +186,13 @@ public function testTo() $this->assertEquals('https://example.com/#test', Url::to('@web5', 'https')); $this->assertEquals('//example.com/#test', Url::to('@web5', '')); + // @see https://github.com/yiisoft/yii2/issues/13156 + \Yii::setAlias('@cdn', '//cdn.example.com'); + $this->assertEquals('http://cdn.example.com/images/logo.gif', Url::to('@cdn/images/logo.gif', 'http')); + $this->assertEquals('//cdn.example.com/images/logo.gif', Url::to('@cdn/images/logo.gif', '')); + $this->assertEquals('https://cdn.example.com/images/logo.gif', Url::to('@cdn/images/logo.gif', 'https')); + \Yii::setAlias('@cdn', null); + //In case there is no controller, throw an exception $this->removeMockedAction();