I do not quite understand how this happens with the variables PUT and Slim.
Please consider the following route:
$app->put('/users/me',function() use ($app){
$app->response->headers->set('Content-Type', 'application/json');
$userData = $app->request()->put();
var_dump($userData);
});
This route basically accepts a PUT request and returns all variables with it. When I test it with curl, it works as expected:
oris@oris:~/slim-api$ curl -i -H "Accept: application/json" -X PUT -d "hello=there" http://slim-api.com/users/me
HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Thu, 06 Feb 2014 09:53:11 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.6-1ubuntu1.5
Set-Cookie: PHPSESSID=2r6ta2fg6vdk7; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
array(1) {
["hello"]=>
string(5) "there"
}
I wrote a small PHPUnit test for this route using this bootstrap.php file :
public function testPutUsersMe()
{
$this->put('/users/me',array("myKey=myValue"));
$userResponse = $this->response->body();
var_dump($userResponse);
}
I also added a tiny addition to this boot file to update superglobals for every request:
public function setRequestGlobals($method,$path,$options = array())
{
$_SERVER['REQUEST_METHOD'] = $method;
if (!empty($options)){
if ($method === "GET"){
foreach($options as $key => $value){
$_GET[$key] = $value;
}
}
if ($method === "POST"){
foreach($options as $key => $value){
$_POST[$key] = $value;
}
}
if ($method === "PUT"){
foreach($options as $key => $value){
$_POST[$key] = $value;
}
}
}
}
I call $this->setRequestGlobals($method,$path,$options);inside the method request. But this brings up the following answer, which is not quite right:
oris@oris:~/slim-api$ phpunit
PHPUnit 3.7.29 by Sebastian Bergmann.
Configuration read from /home/oris/slim-api/phpunit.xml
...........string(44) "Array
(
[slim.input] => myKey=myValue
)
"
Time: 119 ms, Memory: 7.75Mb
OK (11 tests, 85 assertions)
oris@oris:~/slim-api$
, " ", , [slim.input] , . ( ) POST, :
oris@oris:~/slim-api$ phpunit
PHPUnit 3.7.29 by Sebastian Bergmann.
Configuration read from /home/oris/slim-api/phpunit.xml
...........string(33) "Array
(
[myKey] => myValue
)
"
Time: 128 ms, Memory: 7.75Mb
OK (11 tests, 85 assertions)
oris@oris:~/slim-api$
:
, Slim-, /vendor/slim/slim/Slim/Http/Request.php , put POST:
public function put($key = null, $default = null)
{
return $this->post($key, $default);
}