Skip to content

Commit fadc227

Browse files
authored
Merge pull request #6675 from michalsn/fix/getPostGet
fix: getGetPost() and getPostGet() when index is null
2 parents a3eedf4 + 86c91cf commit fadc227

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

system/HTTP/IncomingRequest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,9 @@ public function getPost($index = null, $filter = null, $flags = null)
613613
*/
614614
public function getPostGet($index = null, $filter = null, $flags = null)
615615
{
616+
if ($index === null) {
617+
return array_merge($this->getGet($index, $filter, $flags), $this->getPost($index, $filter, $flags));
618+
}
616619
// Use $_POST directly here, since filter_has_var only
617620
// checks the initial POST data, not anything that might
618621
// have been added since.
@@ -630,6 +633,9 @@ public function getPostGet($index = null, $filter = null, $flags = null)
630633
*/
631634
public function getGetPost($index = null, $filter = null, $flags = null)
632635
{
636+
if ($index === null) {
637+
return array_merge($this->getPost($index, $filter, $flags), $this->getGet($index, $filter, $flags));
638+
}
633639
// Use $_GET directly here, since filter_has_var only
634640
// checks the initial GET data, not anything that might
635641
// have been added since.

tests/system/HTTP/IncomingRequestTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,26 @@ public function testGetPostEmpty()
563563
$this->assertSame($_GET, $this->request->getGetPost());
564564
}
565565

566+
public function testPostGetSecondStream()
567+
{
568+
$_GET['get'] = '3';
569+
$this->assertSame($_GET, $this->request->getPostGet());
570+
}
571+
572+
public function testGetPostSecondStream()
573+
{
574+
$_POST['post'] = '5';
575+
$this->assertSame($_POST, $this->request->getGetPost());
576+
}
577+
578+
public function testGetPostSecondStreams()
579+
{
580+
$_GET['get'] = '3';
581+
$_POST['post'] = '5';
582+
$this->assertSame(array_merge($_GET, $_POST), $this->request->getPostGet());
583+
$this->assertSame(array_merge($_POST, $_GET), $this->request->getGetPost());
584+
}
585+
566586
public function testWithFalseBody()
567587
{
568588
// Use `false` here to simulate file_get_contents returning a false value

user_guide_src/source/changelogs/v4.2.8.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ none.
3636
Bugs Fixed
3737
**********
3838

39-
none.
39+
- Fixed a bug when the ``CodeIgniter\HTTP\IncomingRequest::getPostGet()`` and ``CodeIgniter\HTTP\IncomingRequest::getGetPost()`` methods didn't return values from the other stream when ``index`` was set to ``null``.
4040

4141
See the repo's `CHANGELOG.md <https://github.com/codeigniter4/CodeIgniter4/blob/develop/CHANGELOG.md>`_ for a complete list of bugs fixed.

user_guide_src/source/incoming/incomingrequest.rst

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ The methods provided by the parent classes that are available are:
339339
found `here <https://www.php.net/manual/en/filter.filters.php>`__.
340340
:param int $flags: Flags to apply. A list of flags can be found
341341
`here <https://www.php.net/manual/en/filter.filters.flags.php>`__.
342-
:returns: $_POST if no parameters supplied, otherwise the POST value if found, or null if not
342+
:returns: $_POST and $_GET combined if no parameters specified (prefer POST value on conflict),
343+
otherwise looks for POST value, if nothing found looks for GET value, if no value found returns null
343344
:rtype: mixed|null
344345

345346
This method works pretty much the same way as ``getPost()`` and ``getGet()``, only combined.
@@ -348,22 +349,29 @@ The methods provided by the parent classes that are available are:
348349

349350
.. literalinclude:: incomingrequest/032.php
350351

352+
If no index is specified, it will return both POST and GET streams combined.
353+
Although POST data will be preferred in case of name conflict.
354+
351355
.. php:method:: getGetPost([$index = null[, $filter = null[, $flags = null]]])
352356
353357
:param string $index: The name of the variable/key to look for.
354358
:param int $filter: The type of filter to apply. A list of filters can be
355359
found `here <https://www.php.net/manual/en/filter.filters.php>`__.
356360
:param int $flags: Flags to apply. A list of flags can be found
357361
`here <https://www.php.net/manual/en/filter.filters.flags.php>`__.
358-
:returns: $_POST if no parameters supplied, otherwise the POST value if found, or null if not
362+
:returns: $_GET and $_POST combined if no parameters specified (prefer GET value on conflict),
363+
otherwise looks for GET value, if nothing found looks for POST value, if no value found returns null
359364
:rtype: mixed|null
360365

361366
This method works pretty much the same way as ``getPost()`` and ``getGet()``, only combined.
362-
It will search through both POST and GET streams for data, looking first in GET, and
367+
It will search through both GET and POST streams for data, looking first in GET, and
363368
then in POST:
364369

365370
.. literalinclude:: incomingrequest/033.php
366371

372+
If no index is specified, it will return both GET and POST streams combined.
373+
Although GET data will be preferred in case of name conflict.
374+
367375
.. php:method:: getCookie([$index = null[, $filter = null[, $flags = null]]])
368376
:noindex:
369377

0 commit comments

Comments
 (0)