Skip to content

Commit e733417

Browse files
committed
fix: change behavior
The $routePath never starts with `/`. If the path is `/`, the URI retain the trailing `/`.
1 parent ccfd79d commit e733417

2 files changed

Lines changed: 72 additions & 24 deletions

File tree

‎system/HTTP/SiteURI.php‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class SiteURI extends URI
7575
*
7676
* If the baseURL contains sub folders, this value will be different from
7777
* the current URI path.
78+
*
79+
* This value never starts with '/'.
7880
*/
7981
private string $routePath;
8082

@@ -98,18 +100,27 @@ public function __construct(
98100
$this->setBasePath($baseURL);
99101

100102
$relativePath = URI::removeDotSegments($relativePath);
101-
// Remove starting slash
102-
if ($relativePath !== '' && $relativePath[0] === '/') {
103+
// Remove starting slash unless it is `/`.
104+
if ($relativePath !== '' && $relativePath[0] === '/' && $relativePath !== '/') {
103105
$relativePath = ltrim($relativePath, '/');
104106
}
105107

108+
$tempPath = $relativePath;
109+
106110
// Check for an index page
107111
$indexPage = '';
108112
if ($configApp->indexPage !== '') {
109-
$indexPage = $configApp->indexPage . '/';
113+
$indexPage = $configApp->indexPage;
114+
115+
// Check if we need a separator
116+
if ($relativePath !== '' && $relativePath[0] !== '/' && $relativePath[0] !== '?') {
117+
$indexPage .= '/';
118+
}
119+
} elseif ($relativePath === '/') {
120+
$tempPath = '';
110121
}
111122

112-
$tempUri = $baseURL . $indexPage . $relativePath;
123+
$tempUri = $baseURL . $indexPage . $tempPath;
113124
$uri = new URI($tempUri);
114125

115126
// Update scheme
@@ -305,16 +316,12 @@ public function refreshPath()
305316
$allSegments = array_merge($this->baseSegments, $this->segments);
306317
$this->path = '/' . $this->filterPath(implode('/', $allSegments));
307318

308-
$this->routePath = $this->filterPath(implode('/', $this->segments));
309-
310-
if ($this->routePath === '') {
311-
$this->routePath = '/';
312-
313-
if ($this->indexPage !== '') {
314-
$this->path .= '/';
315-
}
319+
if ($this->routePath === '/' && $this->path !== '/') {
320+
$this->path .= '/';
316321
}
317322

323+
$this->routePath = $this->filterPath(implode('/', $this->segments));
324+
318325
return $this;
319326
}
320327

‎tests/system/HTTP/SiteURITest.php‎

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,22 @@ public function testConstructor()
3232

3333
$uri = new SiteURI($config);
3434

35+
$this->assertInstanceOf(SiteURI::class, $uri);
36+
$this->assertSame('http://example.com/index.php', (string) $uri);
37+
$this->assertSame('', $uri->getRoutePath());
38+
$this->assertSame('/index.php', $uri->getPath());
39+
$this->assertSame('http://example.com/', $uri->getBaseURL());
40+
}
41+
42+
public function testConstructorPathSlash()
43+
{
44+
$config = new App();
45+
46+
$uri = new SiteURI($config, '/');
47+
3548
$this->assertInstanceOf(SiteURI::class, $uri);
3649
$this->assertSame('http://example.com/index.php/', (string) $uri);
37-
$this->assertSame('/', $uri->getRoutePath());
50+
$this->assertSame('', $uri->getRoutePath());
3851
$this->assertSame('/index.php/', $uri->getPath());
3952
$this->assertSame('http://example.com/', $uri->getBaseURL());
4053
}
@@ -107,9 +120,9 @@ public function testConstructorHost()
107120
$uri = new SiteURI($config, '', 'sub.example.com');
108121

109122
$this->assertInstanceOf(SiteURI::class, $uri);
110-
$this->assertSame('http://sub.example.com/index.php/', (string) $uri);
111-
$this->assertSame('/', $uri->getRoutePath());
112-
$this->assertSame('/index.php/', $uri->getPath());
123+
$this->assertSame('http://sub.example.com/index.php', (string) $uri);
124+
$this->assertSame('', $uri->getRoutePath());
125+
$this->assertSame('/index.php', $uri->getPath());
113126
$this->assertSame('http://sub.example.com/', $uri->getBaseURL());
114127
}
115128

@@ -120,7 +133,7 @@ public function testConstructorScheme()
120133
$uri = new SiteURI($config, '', null, 'https');
121134

122135
$this->assertInstanceOf(SiteURI::class, $uri);
123-
$this->assertSame('https://example.com/index.php/', (string) $uri);
136+
$this->assertSame('https://example.com/index.php', (string) $uri);
124137
$this->assertSame('https://example.com/', $uri->getBaseURL());
125138
}
126139

@@ -132,9 +145,9 @@ public function testConstructorSubfolder()
132145
$uri = new SiteURI($config);
133146

134147
$this->assertInstanceOf(SiteURI::class, $uri);
135-
$this->assertSame('http://example.com/ci4/index.php/', (string) $uri);
136-
$this->assertSame('/', $uri->getRoutePath());
137-
$this->assertSame('/ci4/index.php/', $uri->getPath());
148+
$this->assertSame('http://example.com/ci4/index.php', (string) $uri);
149+
$this->assertSame('', $uri->getRoutePath());
150+
$this->assertSame('/ci4/index.php', $uri->getPath());
138151
$this->assertSame('http://example.com/ci4/', $uri->getBaseURL());
139152
}
140153

@@ -158,7 +171,7 @@ public function testConstructorForceGlobalSecureRequests()
158171

159172
$uri = new SiteURI($config);
160173

161-
$this->assertSame('https://example.com/index.php/', (string) $uri);
174+
$this->assertSame('https://example.com/index.php', (string) $uri);
162175
$this->assertSame('https://example.com/', $uri->getBaseURL());
163176
}
164177

@@ -171,7 +184,20 @@ public function testConstructorIndexPageEmpty()
171184

172185
$this->assertSame('http://example.com/', (string) $uri);
173186
$this->assertSame('http://example.com/', $uri->getBaseURL());
174-
$this->assertSame('/', $uri->getRoutePath());
187+
$this->assertSame('', $uri->getRoutePath());
188+
$this->assertSame('/', $uri->getPath());
189+
}
190+
191+
public function testConstructorIndexPageEmptyWithPathSlash()
192+
{
193+
$config = new App();
194+
$config->indexPage = '';
195+
196+
$uri = new SiteURI($config, '/');
197+
198+
$this->assertSame('http://example.com/', (string) $uri);
199+
$this->assertSame('http://example.com/', $uri->getBaseURL());
200+
$this->assertSame('', $uri->getRoutePath());
175201
$this->assertSame('/', $uri->getPath());
176202
}
177203

@@ -242,8 +268,23 @@ public function testSetPathEmpty()
242268

243269
$uri->setPath('');
244270

271+
$this->assertSame('http://example.com/index.php', (string) $uri);
272+
$this->assertSame('', $uri->getRoutePath());
273+
$this->assertSame('/index.php', $uri->getPath());
274+
$this->assertSame([], $uri->getSegments());
275+
$this->assertSame(0, $uri->getTotalSegments());
276+
}
277+
278+
public function testSetPathSlash()
279+
{
280+
$config = new App();
281+
282+
$uri = new SiteURI($config);
283+
284+
$uri->setPath('/');
285+
245286
$this->assertSame('http://example.com/index.php/', (string) $uri);
246-
$this->assertSame('/', $uri->getRoutePath());
287+
$this->assertSame('', $uri->getRoutePath());
247288
$this->assertSame('/index.php/', $uri->getPath());
248289
$this->assertSame([], $uri->getSegments());
249290
$this->assertSame(0, $uri->getTotalSegments());
@@ -322,7 +363,7 @@ public function testGetRoutePath()
322363
$config = new App();
323364
$uri = new SiteURI($config);
324365

325-
$this->assertSame('/', $uri->getRoutePath());
366+
$this->assertSame('', $uri->getRoutePath());
326367
}
327368

328369
public function testGetSegments()

0 commit comments

Comments
 (0)