Skip to content

Commit 77d43a8

Browse files
committed
feat: add param $relativePath to constructor
Change the condition to add / after index.php.
1 parent 5af1578 commit 77d43a8

File tree

3 files changed

+80
-24
lines changed

3 files changed

+80
-24
lines changed

system/HTTP/SiteURI.php

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,31 @@ class SiteURI extends URI
7070
*/
7171
private string $routePath;
7272

73-
public function __construct(App $configApp)
73+
/**
74+
* @param string $relativePath URI path relative to baseURL. May include
75+
* queries or fragments.
76+
*/
77+
public function __construct(App $configApp, string $relativePath = '')
7478
{
75-
// It's possible the user forgot a trailing slash on their
76-
// baseURL, so let's help them out.
77-
$baseURL = rtrim($configApp->baseURL, '/ ') . '/';
78-
79-
// Validate baseURL
80-
if (filter_var($baseURL, FILTER_VALIDATE_URL) === false) {
81-
throw new ConfigException(
82-
'Config\App::$baseURL is invalid.'
83-
);
84-
}
85-
86-
$this->baseURL = $baseURL;
79+
$this->baseURL = $this->normalizeBaseURL($configApp);
8780
$this->indexPage = $configApp->indexPage;
8881

8982
$this->setBaseSegments();
9083

9184
// Check for an index page
9285
$indexPage = '';
9386
if ($configApp->indexPage !== '') {
94-
$indexPage = $configApp->indexPage . '/';
87+
$indexPage = $configApp->indexPage;
88+
89+
// Check if we need a separator
90+
if ($relativePath !== '' && $relativePath[0] !== '/' && $relativePath[0] !== '?') {
91+
$indexPage .= '/';
92+
}
9593
}
9694

97-
$tempUri = $this->baseURL . $indexPage;
95+
$relativePath = URI::removeDotSegments($relativePath);
96+
97+
$tempUri = $this->baseURL . $indexPage . $relativePath;
9898
$uri = new URI($tempUri);
9999

100100
if ($configApp->forceGlobalSecureRequests) {
@@ -107,7 +107,25 @@ public function __construct(App $configApp)
107107
}
108108
$this->applyParts($parts);
109109

110-
$this->setPath('/');
110+
$parts = explode('?', $relativePath);
111+
$routePath = $parts[0];
112+
$this->setRoutePath($routePath);
113+
}
114+
115+
private function normalizeBaseURL(App $configApp): string
116+
{
117+
// It's possible the user forgot a trailing slash on their
118+
// baseURL, so let's help them out.
119+
$baseURL = rtrim($configApp->baseURL, '/ ') . '/';
120+
121+
// Validate baseURL
122+
if (filter_var($baseURL, FILTER_VALIDATE_URL) === false) {
123+
throw new ConfigException(
124+
'Config\App::$baseURL is invalid.'
125+
);
126+
}
127+
128+
return $baseURL;
111129
}
112130

113131
/**
@@ -247,14 +265,22 @@ public function __toString(): string
247265
* @return $this
248266
*/
249267
public function setPath(string $path)
268+
{
269+
$this->setRoutePath($path);
270+
271+
return $this;
272+
}
273+
274+
/**
275+
* Sets the route path (and segments).
276+
*/
277+
private function setRoutePath(string $path): void
250278
{
251279
$this->routePath = $this->filterPath($path);
252280

253281
$this->segments = $this->convertToSegments($this->routePath);
254282

255283
$this->refreshPath();
256-
257-
return $this;
258284
}
259285

260286
/**

system/HTTP/SiteURIFactory.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,17 @@ private function parseQueryString(): string
191191
*/
192192
private function createURIFromRoutePath(string $routePath): SiteURI
193193
{
194-
$uri = new SiteURI($this->appConfig);
194+
$query = $this->server['QUERY_STRING'] ?? '';
195+
196+
$relativePath = $query !== '' ? $routePath . '?' . $query : $routePath;
197+
198+
$uri = new SiteURI($this->appConfig, $relativePath);
195199

196200
// Based on our baseURL and allowedHostnames provided by the developer
197201
// and HTTP_HOST, set our current hostname.
198202
$host = $this->determineHost($uri->getBaseURL());
199203
$uri->setHost($host);
200204

201-
$uri->setPath($routePath);
202-
203-
// Ensure we have any query vars
204-
$uri->setQuery($this->server['QUERY_STRING'] ?? '');
205-
206205
return $uri;
207206
}
208207

tests/system/HTTP/SiteURITest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ public function testConstructor()
3737
$this->assertSame('/index.php/', $uri->getPath());
3838
}
3939

40+
public function testConstructorRelativePath()
41+
{
42+
$config = new App();
43+
44+
$uri = new SiteURI($config, 'one/two');
45+
46+
$this->assertSame('http://example.com/index.php/one/two', (string) $uri);
47+
$this->assertSame('/index.php/one/two', $uri->getPath());
48+
}
49+
50+
public function testConstructorRelativePathWithQuery()
51+
{
52+
$config = new App();
53+
54+
$uri = new SiteURI($config, 'one/two?foo=1&bar=2');
55+
56+
$this->assertSame('http://example.com/index.php/one/two?foo=1&bar=2', (string) $uri);
57+
$this->assertSame('/index.php/one/two', $uri->getPath());
58+
}
59+
4060
public function testConstructorSubfolder()
4161
{
4262
$config = new App();
@@ -49,6 +69,17 @@ public function testConstructorSubfolder()
4969
$this->assertSame('/ci4/index.php/', $uri->getPath());
5070
}
5171

72+
public function testConstructorSubfolderRelativePathWithQuery()
73+
{
74+
$config = new App();
75+
$config->baseURL = 'http://example.com/ci4/';
76+
77+
$uri = new SiteURI($config, 'one/two?foo=1&bar=2');
78+
79+
$this->assertSame('http://example.com/ci4/index.php/one/two?foo=1&bar=2', (string) $uri);
80+
$this->assertSame('/ci4/index.php/one/two', $uri->getPath());
81+
}
82+
5283
public function testConstructorForceGlobalSecureRequests()
5384
{
5485
$config = new App();

0 commit comments

Comments
 (0)