Skip to content

Commit 4f3ba28

Browse files
committed
Fix base URL logic
1 parent 03e0bec commit 4f3ba28

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

system/HTTP/URL.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,26 +39,34 @@ class URL
3939
//--------------------------------------------------------------------
4040

4141
/**
42-
* Returns an instance representing the current URL.
42+
* Returns an instance representing the base URL.
43+
*
44+
* @param string $uri Additional URI string to include
4345
*
4446
* @return static
4547
*/
46-
final public static function base()
48+
final public static function base(string $uri = '')
4749
{
48-
return new static('');
50+
// Base URLs never include the index page
51+
$config = clone config('App');
52+
$config->indexPage = '';
53+
54+
return new static($uri, $config);
4955
}
5056

5157
/**
52-
* Returns an instance representing the current URL.
58+
* Returns an instance representing a routed URL.
5359
*
54-
* @param string $uri
60+
* @param string $uri Named route, reverse route, or URI string
5561
*
5662
* @return static
5763
*/
5864
final public static function to(string $uri)
5965
{
66+
$uri = rtrim($uri, '/ ');
67+
6068
// Check for a named or reverse-route
61-
if ($route = Services::routes()->reverseRoute($uri))
69+
if ($uri !== '' && $route = Services::routes()->reverseRoute($uri))
6270
{
6371
return new static($route);
6472
}

tests/system/HTTP/URLTest.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,16 @@ public function testConstructorPath(string $path, string $expected)
151151
/**
152152
* @dataProvider configProvider
153153
*/
154-
public function testConstructorConfig(array $configs, string $baseURL)
154+
public function testConstructorConfig(array $configs, string $baseURL, string $siteURL)
155155
{
156156
foreach ($configs as $key => $value)
157157
{
158158
$this->setConfig($key, $value);
159159
}
160160

161-
$url = new URL('testaroo', $this->config);
162-
$expected = rtrim($baseURL, '/') . '/testaroo';
161+
$url = new URL('testaroo', $this->config);
163162

164-
$this->assertSame($expected, (string) $url);
163+
$this->assertSame($siteURL, (string) $url);
165164
}
166165

167166
/**
@@ -180,7 +179,7 @@ public function testCurrent(string $uri, string $expected = null)
180179
/**
181180
* @dataProvider configProvider
182181
*/
183-
public function testBase(array $configs, string $expected)
182+
public function testBase(array $configs, string $baseURL, string $siteURL)
184183
{
185184
foreach ($configs as $key => $value)
186185
{
@@ -191,7 +190,14 @@ public function testBase(array $configs, string $expected)
191190

192191
$url = URL::base();
193192

194-
$this->assertSame($expected, (string) $url);
193+
$this->assertSame($baseURL, (string) $url);
194+
}
195+
196+
public function testBaseWithUri()
197+
{
198+
$url = URL::base('images/cat.gif');
199+
200+
$this->assertSame('http://example.com/images/cat.gif', (string) $url);
195201
}
196202

197203
public function testTo()
@@ -270,52 +276,60 @@ public function configProvider(): array
270276
[
271277
'baseURL' => 'http://bananas.com',
272278
],
273-
'http://bananas.com/index.php',
279+
'http://bananas.com/',
280+
'http://bananas.com/index.php/testaroo',
274281
],
275282
[
276283
[
277284
'baseURL' => 'http://bananas.com/',
278285
],
279-
'http://bananas.com/index.php',
286+
'http://bananas.com/',
287+
'http://bananas.com/index.php/testaroo',
280288
],
281289
[
282290
[
283291
'baseURL' => 'http://bananas.com/subfolder/',
284292
],
285-
'http://bananas.com/subfolder/index.php',
293+
'http://bananas.com/subfolder/',
294+
'http://bananas.com/subfolder/index.php/testaroo',
286295
],
287296
[
288297
[
289298
'indexPage' => '',
290299
],
291300
'http://example.com/',
301+
'http://example.com/testaroo',
292302
],
293303
[
294304
[
295305
'baseURL' => 'http://bananas.com/subfolder/',
296306
'indexPage' => '',
297307
],
298308
'http://bananas.com/subfolder/',
309+
'http://bananas.com/subfolder/testaroo',
299310
],
300311
[
301312
[
302313
'forceGlobalSecureRequests' => true,
303314
],
304-
'https://example.com/index.php',
315+
'https://example.com/',
316+
'https://example.com/index.php/testaroo',
305317
],
306318
[
307319
[
308320
'baseURL' => 'http://bananas.com/',
309321
'forceGlobalSecureRequests' => true,
310322
],
311-
'https://bananas.com/index.php',
323+
'https://bananas.com/',
324+
'https://bananas.com/index.php/testaroo',
312325
],
313326
[
314327
[
315328
'baseURL' => 'https://bananas.com/subfolder/',
316329
'forceGlobalSecureRequests' => true,
317330
],
318-
'https://bananas.com/subfolder/index.php',
331+
'https://bananas.com/subfolder/',
332+
'https://bananas.com/subfolder/index.php/testaroo',
319333
],
320334
];
321335
}

0 commit comments

Comments
 (0)