Skip to content

Commit 10eae04

Browse files
committed
feat: add param $relativePath to constructor
Change the condition to add / after index.php.
1 parent e20a693 commit 10eae04

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
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
/**

tests/system/HTTP/SiteURITest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ 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+
$this->assertSame('foo=1&bar=2', $uri->getQuery());
59+
}
60+
4061
public function testConstructorSubfolder()
4162
{
4263
$config = new App();
@@ -49,6 +70,18 @@ public function testConstructorSubfolder()
4970
$this->assertSame('/ci4/index.php/', $uri->getPath());
5071
}
5172

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

0 commit comments

Comments
 (0)