Skip to content

Commit 0f357f2

Browse files
committed
fix: site_url() does not use alt Config
1 parent 912153a commit 0f357f2

2 files changed

Lines changed: 30 additions & 13 deletions

File tree

system/Helpers/url_helper.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
*
2727
* @internal Outside the framework this should not be used directly.
2828
*
29-
* @param string $relativePath May include queries or fragments
29+
* @param string $relativePath May include queries or fragments
30+
* @param App|null $config Alternative Config to use
31+
* @param bool $useConfig Whether to use the Config baseURL
3032
*
3133
* @throws HTTPException For invalid paths.
3234
* @throws InvalidArgumentException For invalid config.
3335
*/
34-
function _get_uri(string $relativePath = '', ?App $config = null): URI
36+
function _get_uri(string $relativePath = '', ?App $config = null, bool $useConfig = false): URI
3537
{
3638
$config ??= config('App');
3739

@@ -57,9 +59,13 @@ function _get_uri(string $relativePath = '', ?App $config = null): URI
5759
$request = Services::request();
5860

5961
/** @var App $config */
60-
$url = $request instanceof CLIRequest
61-
? rtrim($config->baseURL, '/ ') . '/'
62-
: $request->getUri()->getBaseURL();
62+
if ($useConfig) {
63+
$url = rtrim($config->baseURL, '/ ') . '/';
64+
} else {
65+
$url = $request instanceof CLIRequest
66+
? rtrim($config->baseURL, '/ ') . '/'
67+
: $request->getUri()->getBaseURL();
68+
}
6369

6470
// Check for an index page
6571
if ($config->indexPage !== '') {
@@ -88,18 +94,28 @@ function _get_uri(string $relativePath = '', ?App $config = null): URI
8894
/**
8995
* Returns a site URL as defined by the App config.
9096
*
91-
* @param array|string $relativePath URI string or array of URI segments
92-
* @param string|null $scheme URI scheme. E.g., http, ftp
93-
* @param App|null $config Alternate configuration to use
97+
* @param array|string $relativePath URI string or array of URI segments
98+
* @param string|null $scheme URI scheme. E.g., http, ftp
99+
* @param App|null $config Alternate configuration to use
100+
* @param bool $dontUseConfig Set true if you don't use the Config
101+
* baseURL even when you pass the Config
94102
*/
95-
function site_url($relativePath = '', ?string $scheme = null, ?App $config = null): string
96-
{
103+
function site_url(
104+
$relativePath = '',
105+
?string $scheme = null,
106+
?App $config = null,
107+
bool $dontUseConfig = false
108+
): string {
109+
// If $dontUseConfig is false and $config is not passed,
110+
// we use the Config baseURL.
111+
$useConfig = (! $dontUseConfig) && ($config !== null);
112+
97113
// Convert array of segments to a string
98114
if (is_array($relativePath)) {
99115
$relativePath = implode('/', $relativePath);
100116
}
101117

102-
$uri = _get_uri($relativePath, $config);
118+
$uri = _get_uri($relativePath, $config, $useConfig);
103119

104120
return URI::createURIString(
105121
$scheme ?? $uri->getScheme(),
@@ -124,7 +140,7 @@ function base_url($relativePath = '', ?string $scheme = null): string
124140
$config = clone config('App');
125141
$config->indexPage = '';
126142

127-
return site_url($relativePath, $scheme, $config);
143+
return site_url($relativePath, $scheme, $config, true);
128144
}
129145
}
130146

tests/system/Helpers/URLHelper/SiteUrlTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,15 @@ public function testSiteURLWithAllowedHostname()
322322

323323
$this->config->baseURL = 'http://example.com/public/';
324324
$this->config->allowedHostnames = ['www.example.jp'];
325+
Services::injectMock('config', $this->config);
325326

326327
// URI object are updated in IncomingRequest constructor.
327328
$request = Services::incomingrequest($this->config);
328329
Services::injectMock('request', $request);
329330

330331
$this->assertSame(
331332
'http://www.example.jp/public/index.php/controller/method',
332-
site_url('controller/method', null, $this->config)
333+
site_url('controller/method')
333334
);
334335
}
335336

0 commit comments

Comments
 (0)