38

How I can get all get/ put/ post variables like in Slim 2 for Slim 3?

Slim 2,

$allGetVars = $app->request->get();
$allPutVars = $app->request->put();
$allPostVars = $app->request->post();

How can I do that in Slim 3?

And, for example, http://example.com/books/1?title=hello&content=world

How can I get the params in title and content in Slim 3 now?

Slim 2,

$title = $app->request->get('title');
$content = $app->request->get('content');

How can I do that in Slim 3?

alexw
  • 7,044
  • 6
  • 46
  • 81
laukok
  • 47,545
  • 146
  • 388
  • 689

4 Answers4

76

Get all get/put/post parameters:

//GET
$allGetVars = $request->getQueryParams();
foreach($allGetVars as $key => $param){
   //GET parameters list
}

//POST or PUT
$allPostPutVars = $request->getParsedBody();
foreach($allPostPutVars as $key => $param){
   //POST or PUT parameters list
}

Single parameters value:

//Single GET parameter
$getParam = $allGetVars['title'];

//Single POST/PUT parameter
$postParam = $allPostPutVars['postParam'];
Davide Pastore
  • 8,317
  • 10
  • 37
  • 49
  • 1
    Thank you for the answer! – laukok Sep 19 '15 at 16:32
  • 1
    Thanks mate! Having a hard time looking for this – wobsoriano Mar 20 '16 at 15:32
  • Hello. It doesn't work for me. The only thing who "works" is `$request->getHeaders();` and it adds _HTTP__ as a prefix. For example, if I use _length_ as a post parameters, with `getHeaders()` I'll get _HTTP_LENGTH_. I don't understand why. And `getParsedBody()` return basically _NULL_. Thanks! PS: The strangest thing is: if I do `$request->hasHeader('length')`, i get the value of my parameter. – TDK May 04 '16 at 12:06
  • @TDK How are you sending the request and which version of Slim are you using? – Davide Pastore May 04 '16 at 12:33
  • @Davide I'm using Slim 3, and i'm sending the POST request through [Advanced REST client](https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo) to test it. – TDK May 04 '16 at 14:08
  • @TDK are you sure you have the `Content-type` header set to `application/x-www-form-urlencoded`? – Davide Pastore May 04 '16 at 14:13
  • @Davide Yes. I tried with other content-type juste to try. but still nothing. The result is `array(0) {}` and the request headers are this: `Start_lng: 2.6423183977058 Start_lat: 56.865296679535 Size: 0 Content-Type: application/x-www-form-urlencoded` – TDK May 04 '16 at 14:23
  • @TDK Maybe you could try to create a new question for this. Also add your exact version of Slim, your server and your `.htaccess` file. – Davide Pastore May 04 '16 at 14:37
  • @Davide Ok thanks. I did it here if you want: http://stackoverflow.com/questions/37031274/get-post-put-parameters-with-slim-3. – TDK May 04 '16 at 14:54
  • Adding that the route signature in this case is `$app->get('/books/1', function ($request, $response, $args) { ...}` – Biguá Oct 20 '16 at 17:37
  • 2
    Why is it so complicated? I could as well just use plain old `$_GET` – Gherman Oct 06 '17 at 12:15
8

To Get all request params:

$request->getParams() 
nogo0d
  • 181
  • 1
  • 2
  • 6
4

Request Uri: getQueryParams()

Request Body: getBody()/getParsedBody()

It's not exactly what you are looking for but it comes pretty close.

Martin
  • 2,170
  • 13
  • 32
-1

You can use the map() method to combine get, post & put into a single route.

$app->map(['GET', 'POST', 'PUT'], function(Request $request, Response $response, array $args)) { }

The first argument is an array of the HTTP methods that you want to match. The second parameter is the function that handles the request; pass a request, response and an array of arguments.

Mister Moody
  • 116
  • 1
  • 10