Skip to content

Commit d3c192a

Browse files
authored
Merge pull request #7353 from kenjis/fix-site-url-protocol-relative-link
fix: site_url() does not support protocol-relative links
2 parents 897980b + 96d81ca commit d3c192a

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

system/Helpers/url_helper.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,20 @@ function site_url($relativePath = '', ?string $scheme = null, ?App $config = nul
117117
{
118118
$uri = _get_uri($relativePath, $config);
119119

120-
return URI::createURIString(
120+
$uriString = URI::createURIString(
121121
$scheme ?? $uri->getScheme(),
122122
$uri->getAuthority(),
123123
$uri->getPath(),
124124
$uri->getQuery(),
125125
$uri->getFragment()
126126
);
127+
128+
// For protocol-relative links
129+
if ($scheme === '') {
130+
$uriString = '//' . $uriString;
131+
}
132+
133+
return $uriString;
127134
}
128135
}
129136

tests/system/Helpers/URLHelper/SiteUrlTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,21 @@ public function configProvider()
304304
];
305305
}
306306

307-
// base_url
307+
public function testSiteURLWithEmptyStringScheme()
308+
{
309+
$this->config->baseURL = 'http://example.com/';
310+
$this->config->indexPage = 'index.php';
311+
$this->config->forceGlobalSecureRequests = false;
312+
313+
$this->assertSame(
314+
'//example.com/index.php/test',
315+
site_url('test', '', $this->config)
316+
);
317+
$this->assertSame(
318+
'//example.com/img/test.jpg',
319+
base_url('img/test.jpg', '')
320+
);
321+
}
308322

309323
/**
310324
* These tests are only really relevant to show that base_url()

0 commit comments

Comments
 (0)