Skip to content

Commit ea1b628

Browse files
authored
Merge pull request #7200 from kenjis/fix-base_url
fix: base_url() removes trailing slash in baseURL
2 parents 26605ad + c3489f0 commit ea1b628

6 files changed

Lines changed: 108 additions & 11 deletions

File tree

system/Helpers/url_helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function base_url($relativePath = '', ?string $scheme = null): string
124124
$config = clone config('App');
125125
$config->indexPage = '';
126126

127-
return rtrim(site_url($relativePath, $scheme, $config), '/');
127+
return site_url($relativePath, $scheme, $config);
128128
}
129129
}
130130

tests/system/Helpers/URLHelper/SiteUrlTest.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,27 +59,31 @@ protected function tearDown(): void
5959
* @param bool $secure
6060
* @param string $path
6161
* @param string $expectedSiteUrl
62+
* @param string $expectedBaseUrl
6263
*
6364
* @dataProvider configProvider
6465
*/
65-
public function testUrls($baseURL, $indexPage, $scheme, $secure, $path, $expectedSiteUrl)
66-
{
66+
public function testUrls(
67+
$baseURL,
68+
$indexPage,
69+
$scheme,
70+
$secure,
71+
$path,
72+
$expectedSiteUrl,
73+
$expectedBaseUrl
74+
) {
6775
// Set the config
6876
$this->config->baseURL = $baseURL;
6977
$this->config->indexPage = $indexPage;
7078
$this->config->forceGlobalSecureRequests = $secure;
7179

7280
$this->assertSame($expectedSiteUrl, site_url($path, $scheme, $this->config));
73-
74-
// base_url is always the trimmed site_url without index page
75-
$expectedBaseUrl = $indexPage === '' ? $expectedSiteUrl : str_replace('/' . $indexPage, '', $expectedSiteUrl);
76-
$expectedBaseUrl = rtrim($expectedBaseUrl, '/');
7781
$this->assertSame($expectedBaseUrl, base_url($path, $scheme));
7882
}
7983

8084
public function configProvider()
8185
{
82-
// baseURL, indexPage, scheme, secure, path, expectedSiteUrl
86+
// baseURL, indexPage, scheme, secure, path, expectedSiteUrl, expectedBaseUrl
8387
return [
8488
'forceGlobalSecure' => [
8589
'http://example.com/',
@@ -88,6 +92,7 @@ public function configProvider()
8892
true,
8993
'',
9094
'https://example.com/index.php',
95+
'https://example.com/',
9196
],
9297
[
9398
'http://example.com/',
@@ -96,14 +101,16 @@ public function configProvider()
96101
false,
97102
'',
98103
'http://example.com/index.php',
104+
'http://example.com/',
99105
],
100-
[
106+
'baseURL missing /' => [
101107
'http://example.com',
102108
'index.php',
103109
null,
104110
false,
105111
'',
106112
'http://example.com/index.php',
113+
'http://example.com/',
107114
],
108115
[
109116
'http://example.com/',
@@ -112,6 +119,7 @@ public function configProvider()
112119
false,
113120
'',
114121
'http://example.com/',
122+
'http://example.com/',
115123
],
116124
[
117125
'http://example.com/',
@@ -120,6 +128,7 @@ public function configProvider()
120128
false,
121129
'',
122130
'http://example.com/banana.php',
131+
'http://example.com/',
123132
],
124133
[
125134
'http://example.com/',
@@ -128,6 +137,7 @@ public function configProvider()
128137
false,
129138
'abc',
130139
'http://example.com/abc',
140+
'http://example.com/abc',
131141
],
132142
'URL decode' => [
133143
'http://example.com/',
@@ -136,6 +146,7 @@ public function configProvider()
136146
false,
137147
'template/meet-%26-greet',
138148
'http://example.com/template/meet-&-greet',
149+
'http://example.com/template/meet-&-greet',
139150
],
140151
'URL encode' => [
141152
'http://example.com/',
@@ -144,6 +155,7 @@ public function configProvider()
144155
false,
145156
'<s>alert</s>',
146157
'http://example.com/%3Cs%3Ealert%3C/s%3E',
158+
'http://example.com/%3Cs%3Ealert%3C/s%3E',
147159
],
148160
[
149161
'http://example.com/public/',
@@ -152,6 +164,7 @@ public function configProvider()
152164
false,
153165
'',
154166
'http://example.com/public/index.php',
167+
'http://example.com/public/',
155168
],
156169
[
157170
'http://example.com/public/',
@@ -160,6 +173,7 @@ public function configProvider()
160173
false,
161174
'',
162175
'http://example.com/public/',
176+
'http://example.com/public/',
163177
],
164178
[
165179
'http://example.com/public',
@@ -168,6 +182,7 @@ public function configProvider()
168182
false,
169183
'',
170184
'http://example.com/public/',
185+
'http://example.com/public/',
171186
],
172187
[
173188
'http://example.com/public',
@@ -176,6 +191,7 @@ public function configProvider()
176191
false,
177192
'/',
178193
'http://example.com/public/index.php/',
194+
'http://example.com/public/',
179195
],
180196
[
181197
'http://example.com/public/',
@@ -184,6 +200,7 @@ public function configProvider()
184200
false,
185201
'/',
186202
'http://example.com/public/index.php/',
203+
'http://example.com/public/',
187204
],
188205
[
189206
'http://example.com/',
@@ -192,6 +209,7 @@ public function configProvider()
192209
false,
193210
'foo',
194211
'http://example.com/index.php/foo',
212+
'http://example.com/foo',
195213
],
196214
[
197215
'http://example.com/',
@@ -200,6 +218,7 @@ public function configProvider()
200218
false,
201219
'0',
202220
'http://example.com/index.php/0',
221+
'http://example.com/0',
203222
],
204223
[
205224
'http://example.com/public',
@@ -208,6 +227,7 @@ public function configProvider()
208227
false,
209228
'foo',
210229
'http://example.com/public/index.php/foo',
230+
'http://example.com/public/foo',
211231
],
212232
[
213233
'http://example.com/',
@@ -216,6 +236,7 @@ public function configProvider()
216236
false,
217237
'foo?bar=bam',
218238
'http://example.com/index.php/foo?bar=bam',
239+
'http://example.com/foo?bar=bam',
219240
],
220241
[
221242
'http://example.com/',
@@ -224,6 +245,7 @@ public function configProvider()
224245
false,
225246
'test#banana',
226247
'http://example.com/index.php/test#banana',
248+
'http://example.com/test#banana',
227249
],
228250
[
229251
'http://example.com/',
@@ -232,6 +254,7 @@ public function configProvider()
232254
false,
233255
'foo',
234256
'ftp://example.com/index.php/foo',
257+
'ftp://example.com/foo',
235258
],
236259
[
237260
'http://example.com/',
@@ -240,6 +263,7 @@ public function configProvider()
240263
false,
241264
'news/local/123',
242265
'http://example.com/index.php/news/local/123',
266+
'http://example.com/news/local/123',
243267
],
244268
[
245269
'http://example.com/',
@@ -248,6 +272,7 @@ public function configProvider()
248272
false,
249273
['news', 'local', '123'],
250274
'http://example.com/index.php/news/local/123',
275+
'http://example.com/news/local/123',
251276
],
252277
];
253278
}
@@ -267,12 +292,12 @@ public function testBaseURLDiscovery()
267292
$_SERVER['HTTP_HOST'] = 'example.com';
268293
$_SERVER['REQUEST_URI'] = '/test';
269294

270-
$this->assertSame('http://example.com', base_url());
295+
$this->assertSame('http://example.com/', base_url());
271296

272297
$_SERVER['HTTP_HOST'] = 'example.com';
273298
$_SERVER['REQUEST_URI'] = '/test/page';
274299

275-
$this->assertSame('http://example.com', base_url());
300+
$this->assertSame('http://example.com/', base_url());
276301
$this->assertSame('http://example.com/profile', base_url('profile'));
277302
}
278303

user_guide_src/source/changelogs/v4.3.2.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ Release Date: Unreleased
1212
BREAKING
1313
********
1414

15+
Behavior Changes
16+
================
17+
18+
base_url()
19+
----------
20+
21+
Due to a bug, in previous versions :php:func:`base_url()` without argument returned baseURL
22+
without a trailing slash (``/``) like ``http://localhost:8080``. Now it returns
23+
baseURL with a trailing slash. This is the same behavior as ``base_url()`` in
24+
CodeIgniter 3.
25+
1526
Message Changes
1627
***************
1728

user_guide_src/source/helpers/url_helper.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ The following functions are available:
6464
.. note:: Since v4.3.0, if you set ``Config\App::$allowedHostnames``,
6565
this returns the URL with the hostname set in it if the current URL matches.
6666

67+
.. note:: In previous versions, this returned the base URL without a trailing
68+
slash (``/``) when called with no argument. The bug was fixed and
69+
since v4.3.2 it returns the base URL with a trailing slash.
70+
6771
Returns your site base URL, as specified in your config file. Example:
6872

6973
.. literalinclude:: url_helper/003.php
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
##############################
2+
Upgrading from 4.3.1 to 4.3.2
3+
##############################
4+
5+
Please refer to the upgrade instructions corresponding to your installation method.
6+
7+
- :ref:`Composer Installation App Starter Upgrading <app-starter-upgrading>`
8+
- :ref:`Composer Installation Adding CodeIgniter4 to an Existing Project Upgrading <adding-codeigniter4-upgrading>`
9+
- :ref:`Manual Installation Upgrading <installing-manual-upgrading>`
10+
11+
.. contents::
12+
:local:
13+
:depth: 2
14+
15+
Breaking Changes
16+
****************
17+
18+
base_url()
19+
==========
20+
21+
The :php:func:`base_url()` behavior has been fix. In previous versions, when you
22+
call ``base_url()`` **without argument**, it returned baseURL without a trailing
23+
slash (``/``). Now it returns baseURL with a trailing slash. For example:
24+
25+
- before: ``http://example.com``
26+
- after: ``http://example.com/``
27+
28+
If you have code to call ``base_url()`` without argument, you may need to adjust the URLs.
29+
30+
Project Files
31+
*************
32+
33+
Some files in the **project space** (root, app, public, writable) received updates. Due to
34+
these files being outside of the **system** scope they will not be changed without your intervention.
35+
36+
There are some third-party CodeIgniter modules available to assist with merging changes to
37+
the project space: `Explore on Packagist <https://packagist.org/explore/?query=codeigniter4%20updates>`_.
38+
39+
Content Changes
40+
===============
41+
42+
The following files received significant changes (including deprecations or visual adjustments)
43+
and it is recommended that you merge the updated versions with your application:
44+
45+
Config
46+
------
47+
48+
- @TODO
49+
50+
All Changes
51+
===========
52+
53+
This is a list of all files in the **project space** that received changes;
54+
many will be simple comments or formatting that have no effect on the runtime:
55+
56+
- @TODO

user_guide_src/source/installation/upgrading.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ See also :doc:`./backward_compatibility_notes`.
1616

1717
backward_compatibility_notes
1818

19+
upgrade_432
1920
upgrade_431
2021
upgrade_430
2122
upgrade_4212

0 commit comments

Comments
 (0)