Skip to content

Commit

Permalink
Fix protocol-relative URLs in Url::to() (#13156) (#13157)
Browse files Browse the repository at this point in the history
* Fix protocol-relative URLs in `Url::to()`

* Use `isRelative()` in `Url::to()`
  • Loading branch information
rob006 authored and SilverFire committed Dec 11, 2016
1 parent 2645b5a commit 8e0af24
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion framework/helpers/BaseUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/');
}
Expand Down
7 changes: 7 additions & 0 deletions tests/framework/helpers/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 8e0af24

Please sign in to comment.