Slim 3 - how to get all get/ put/ post variables?
Asked Answered
E

4

41

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?

Emmanuelemmeline answered 19/9, 2015 at 12:25 Comment(0)
P
79

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'];
Playlet answered 19/9, 2015 at 14:8 Comment(9)
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.Dropforge
@Dropforge How are you sending the request and which version of Slim are you using?Playlet
@Davide I'm using Slim 3, and i'm sending the POST request through Advanced REST client to test it.Dropforge
@Dropforge are you sure you have the Content-type header set to application/x-www-form-urlencoded?Playlet
@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-urlencodedDropforge
@Dropforge Maybe you could try to create a new question for this. Also add your exact version of Slim, your server and your .htaccess file.Playlet
@Davide Ok thanks. I did it here if you want: #37031774.Dropforge
Adding that the route signature in this case is $app->get('/books/1', function ($request, $response, $args) { ...}Verine
Why is it so complicated? I could as well just use plain old $_GETCompressive
S
8

To Get all request params:

$request->getParams() 
Sinistrodextral answered 7/5, 2016 at 23:20 Comment(0)
U
6

Request Uri: getQueryParams()

Request Body: getBody()/getParsedBody()

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

Useful answered 19/9, 2015 at 13:52 Comment(0)
P
-2

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.

Parabolic answered 25/2, 2019 at 0:24 Comment(1)
this does not answer the question.Marya

© 2022 - 2024 — McMap. All rights reserved.