Closed phm46 closed 8 years ago
I couldn't reproduce your issue.
This code:
<?php
chdir(dirname(__DIR__));
require 'vendor/autoload.php';
use Slim\Http\Environment;
use Slim\Http\Request;
// Creating request with query params and parsed body
$request = Request::createFromEnvironment(Environment::mock());
$request = $request->withQueryParams(['foo' => 'bar'])->withParsedBody(['bar' => 'foo']);
echo 'With data:' . PHP_EOL;
var_dump($request->getQueryParams());
var_dump($request->getParsedBody());
echo PHP_EOL;
// Attempting to clear
$request = $request->withQueryParams(Array())->withParsedBody(Array());
echo 'Without data:' . PHP_EOL;
var_dump($request->getQueryParams());
var_dump($request->getParsedBody());
Gave:
With data:
array(1) {
'foo' =>
string(3) "bar"
}
array(1) {
'bar' =>
string(3) "foo"
}
Without data:
array(0) {
}
array(0) {
}
Could you modify this script to reproduce your issue?
Yes, that is true. But this bug happens only if you use normal request with query and body params. If you manualy set these params with
$request = $request->withQueryParams(['foo' => 'bar'])->withParsedBody(['bar' => 'foo']);
it works corectly. Try this code:
<?php
chdir(dirname(__DIR__));
require 'vendor/autoload.php';
$app = new \Slim\App;
use Slim\Http\Environment;
use Slim\Http\Request;
$app->get('/', function($request, $response, $args){
echo 'With data:' . PHP_EOL;
var_dump($request->getQueryParams());
var_dump($request->getParsedBody());
echo 'Without data:' . PHP_EOL;
$request = $request->withQueryParams(Array())->withParsedBody(Array());
var_dump($request->getQueryParams());
var_dump($request->getParsedBody());
});
$app->run();
and call it from browser as script.php/?foo=bar&bar=foo
Confirmed the bug, this is the offending code: https://github.com/slimphp/Slim/blob/3.x/Slim/Http/Request.php#L728-L741
Here is a fix for getQueryParams
: https://github.com/slimphp/Slim/pull/1732
There needs to be a similar PR for getParsedBody
.
Fixed.
If I set empty array to function withQueryParams and withParsedBody in middleware like this:
it returns same values but it was supposed to return empty array.