Skip to content

Commit 3163d3b

Browse files
authored
Merge pull request #7135 from kenjis/fix-uri_string
fix: remove parameter $relative in `uri_string()`
2 parents 4b9498d + cee174c commit 3163d3b

5 files changed

Lines changed: 60 additions & 55 deletions

File tree

system/Helpers/url_helper.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,14 @@ function previous_url(bool $returnObject = false)
183183
/**
184184
* URL String
185185
*
186-
* Returns the path part of the current URL
187-
*
188-
* @param bool $relative Whether the resulting path should be relative to baseURL
186+
* Returns the path part (relative to baseURL) of the current URL
189187
*/
190-
function uri_string(bool $relative = false): string
188+
function uri_string(): string
191189
{
192-
return $relative
193-
? ltrim(Services::request()->getPath(), '/')
194-
: Services::request()->getUri()->getPath();
190+
// The value of Services::request()->getUri()->getPath() is overridden
191+
// by IncomingRequest constructor. If we use it here, the current tests
192+
// in CurrentUrlTest will fail.
193+
return ltrim(Services::request()->getPath(), '/');
195194
}
196195
}
197196

@@ -583,7 +582,7 @@ function url_is(string $path): bool
583582
{
584583
// Setup our regex to allow wildcards
585584
$path = '/' . trim(str_replace('*', '(\S)*', $path), '/ ');
586-
$currentPath = '/' . trim(uri_string(true), '/ ');
585+
$currentPath = '/' . trim(uri_string(), '/ ');
587586

588587
return (bool) preg_match("|^{$path}$|", $currentPath, $matches);
589588
}

tests/system/Helpers/URLHelper/CurrentUrlTest.php

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function testCurrentURLWithPortInSubfolder()
163163
$this->assertSame(8080, $uri->getPort());
164164
}
165165

166-
public function testUriStringAbsolute()
166+
public function testUriString()
167167
{
168168
$_SERVER['HTTP_HOST'] = 'example.com';
169169
$_SERVER['REQUEST_URI'] = '/assets/image.jpg';
@@ -183,18 +183,7 @@ private function setService(string $uri): void
183183
Services::injectMock('request', $request);
184184
}
185185

186-
public function testUriStringRelative()
187-
{
188-
$_SERVER['HTTP_HOST'] = 'example.com';
189-
$_SERVER['REQUEST_URI'] = '/assets/image.jpg';
190-
191-
$uri = 'http://example.com/assets/image.jpg';
192-
$this->setService($uri);
193-
194-
$this->assertSame('assets/image.jpg', uri_string(true));
195-
}
196-
197-
public function testUriStringNoTrailingSlashAbsolute()
186+
public function testUriStringNoTrailingSlash()
198187
{
199188
$_SERVER['HTTP_HOST'] = 'example.com';
200189
$_SERVER['REQUEST_URI'] = '/assets/image.jpg';
@@ -207,33 +196,12 @@ public function testUriStringNoTrailingSlashAbsolute()
207196
$this->assertSame('assets/image.jpg', uri_string());
208197
}
209198

210-
public function testUriStringNoTrailingSlashRelative()
211-
{
212-
$_SERVER['HTTP_HOST'] = 'example.com';
213-
$_SERVER['REQUEST_URI'] = '/assets/image.jpg';
214-
215-
$this->config->baseURL = 'http://example.com';
216-
217-
$uri = 'http://example.com/assets/image.jpg';
218-
$this->setService($uri);
219-
220-
$this->assertSame('assets/image.jpg', uri_string(true));
221-
}
222-
223-
public function testUriStringEmptyAbsolute()
199+
public function testUriStringEmpty()
224200
{
225201
$uri = 'http://example.com/';
226202
$this->setService($uri);
227203

228-
$this->assertSame('/', uri_string());
229-
}
230-
231-
public function testUriStringEmptyRelative()
232-
{
233-
$uri = 'http://example.com/';
234-
$this->setService($uri);
235-
236-
$this->assertSame('', uri_string(true));
204+
$this->assertSame('', uri_string());
237205
}
238206

239207
public function testUriStringSubfolderAbsolute()
@@ -260,7 +228,7 @@ public function testUriStringSubfolderRelative()
260228
$uri = 'http://example.com/subfolder/assets/image.jpg';
261229
$this->setService($uri);
262230

263-
$this->assertSame('assets/image.jpg', uri_string(true));
231+
$this->assertSame('assets/image.jpg', uri_string());
264232
}
265233

266234
public function urlIsProvider()

user_guide_src/source/changelogs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ See all the changes.
1212
.. toctree::
1313
:titlesonly:
1414

15+
v4.3.2
1516
v4.3.1
1617
v4.3.0
1718
v4.2.12
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Version 4.3.2
2+
#############
3+
4+
Release Date: Unreleased
5+
6+
**4.3.2 release of CodeIgniter4**
7+
8+
.. contents::
9+
:local:
10+
:depth: 3
11+
12+
BREAKING
13+
********
14+
15+
Message Changes
16+
***************
17+
18+
Changes
19+
*******
20+
21+
- The parameter ``$relative`` in :php:func:`uri_string()` was removed. Due to a bug,
22+
the function always returned a path relative to baseURL. No behavior changes.
23+
24+
Deprecations
25+
************
26+
27+
Bugs Fixed
28+
**********
29+
30+
See the repo's
31+
`CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_
32+
for a complete list of bugs fixed.

user_guide_src/source/helpers/url_helper.rst

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,32 @@ The following functions are available:
121121
use a known and trusted source. If the session hasn't been loaded, or is otherwise unavailable,
122122
then a sanitized version of HTTP_REFERER will be used.
123123

124-
.. php:function:: uri_string([$relative = false])
124+
.. php:function:: uri_string()
125125
126-
:param boolean $relative: True if you would like the string relative to baseURL
127126
:returns: A URI string
128-
:rtype: string
127+
:rtype: string
128+
129+
Returns the path part of the current URL relative to baseURL.
129130

130-
Returns the path part of the current URL.
131-
For example, if your URL was this::
131+
For example, when your baseURL is **http://some-site.com/** and the current URL is::
132132

133133
http://some-site.com/blog/comments/123
134134

135135
The function would return::
136136

137-
/blog/comments/123
137+
blog/comments/123
138+
139+
When your baseURL is **http://some-site.com/subfolder/** and the current URL is::
138140

139-
Or with the optional relative parameter::
141+
http://some-site.com/subfolder/blog/comments/123
142+
143+
The function would return::
140144

141-
app.baseURL = http://some-site.com/subfolder/
145+
blog/comments/123
142146

143-
uri_string(); // "/subfolder/blog/comments/123"
144-
uri_string(true); // "blog/comments/123"
147+
.. note:: In previous versions, the parameter ``$relative = false`` was defined.
148+
However, due to a bug, this function always returned a path relative to baseURL.
149+
Since v4.3.2, the parameter has been removed.
145150

146151
.. php:function:: index_page([$altConfig = null])
147152

0 commit comments

Comments
 (0)