Skip to content

Commit 35b0eb9

Browse files
authored
fix for empty PUT/DELETE/PATCH etc requests by parsing values into global _QUERY (#15)
1 parent a3c74f6 commit 35b0eb9

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

src/Nono/Request.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
* @method string|mixed get(string $name = null, mixed $default = null)
1010
* @method string|mixed post(string $name = null, mixed $default = null)
11+
* @method string|mixed query(string $name = null, mixed $default = null)
1112
* @method string|mixed request(string $name = null, mixed $default = null)
1213
* @method string|mixed server(string $name = null, mixed $default = null)
1314
* @method string|mixed session(string $name = null, mixed $default = null)
@@ -17,31 +18,34 @@
1718
class Request
1819
{
1920
/**
20-
* Just triggering the super global.
21+
* Just triggering the super global and fill the query global.
2122
*/
2223
public function __construct()
2324
{
2425
$_SERVER;
26+
if (!in_array($this->method(), ['GET', 'POST'])) {
27+
parse_str(file_get_contents('php://input'), $GLOBALS['_QUERY']);
28+
}
2529
}
2630

2731
/**
2832
* Return URI with query string.
2933
*
3034
* @return string
3135
*/
32-
public function uri()
36+
public function uriWithQuery()
3337
{
34-
return urldecode($this->plainUri());
38+
return urldecode($this->server('REQUEST_URI'));
3539
}
3640

3741
/**
3842
* Return URI without query string.
3943
*
4044
* @return string
4145
*/
42-
public function plainUri()
46+
public function uri()
4347
{
44-
return explode('?', $this->server('REQUEST_URI'))[0];
48+
return explode('?', $this->uriWithQuery())[0];
4549
}
4650

4751
/**

tests/ApplicationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function testRun()
9999

100100
$app = new Application();
101101
$app->get('/profile/{id}', function (Request $request, $id) {
102-
echo $request->plainUri() . ' has id ' . $id;
102+
echo $request->uriWithQuery() . ' has id ' . $id;
103103
});
104104
self::assertEquals('/profile/123 has id 123', $app->run());
105105
}

tests/RequestTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public function testUri()
2727
self::assertEquals('/profile/123', $this->request->uri());
2828
}
2929

30+
public function testUriWithQuery()
31+
{
32+
self::assertEquals('/profile/123?show=settings', $this->request->uriWithQuery());
33+
}
34+
3035
public function testIsHttps()
3136
{
3237
self::assertEquals(false, $this->request->isHttps());

0 commit comments

Comments
 (0)