Skip to content

Commit ff0321f

Browse files
committed
refactor: current_url()
_get_uri() is no longer used.
1 parent 6c410f5 commit ff0321f

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

system/Helpers/url_helper.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
*
3333
* @throws HTTPException For invalid paths.
3434
* @throws InvalidArgumentException For invalid config.
35+
*
36+
* @deprecated No longer used.
3537
*/
3638
function _get_uri(string $relativePath = '', ?App $config = null, bool $useConfig = false): URI
3739
{
@@ -224,7 +226,11 @@ function current_url(bool $returnObject = false, ?IncomingRequest $request = nul
224226
$routePath = $request->getPath();
225227
$currentURI = $request->getUri();
226228

229+
$url = $request->getUri()->getBaseURL();
230+
231+
$config = config('App');
227232
$relativePath = $routePath;
233+
228234
// Append queries and fragments
229235
if ($query = $currentURI->getQuery()) {
230236
$relativePath .= '?' . $query;
@@ -233,7 +239,17 @@ function current_url(bool $returnObject = false, ?IncomingRequest $request = nul
233239
$relativePath .= '#' . $fragment;
234240
}
235241

236-
$uri = _get_uri($relativePath);
242+
// Check for an index page
243+
if ($config->indexPage !== '') {
244+
$url .= $config->indexPage;
245+
246+
// Check if we need a separator
247+
if ($relativePath !== '' && $relativePath[0] !== '/' && $relativePath[0] !== '?') {
248+
$url .= '/';
249+
}
250+
}
251+
252+
$uri = new URI($url . $relativePath);
237253

238254
return $returnObject ? $uri : URI::createURIString($uri->getScheme(), $uri->getAuthority(), $uri->getPath());
239255
}

tests/system/Helpers/URLHelper/CurrentUrlTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function testCurrentURLReturnsBasicURL()
6363

6464
$this->config->baseURL = 'http://example.com/public';
6565

66+
// URI object are updated in IncomingRequest constructor.
67+
$request = Services::incomingrequest($this->config);
68+
Services::injectMock('request', $request);
69+
6670
$this->assertSame('http://example.com/public/index.php/', current_url());
6771
}
6872

0 commit comments

Comments
 (0)