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

落爺英雄遲暮 提交于 2019-11-30 10:24:14

问题


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?


回答1:


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'];



回答2:


To Get all request params:

$request->getParams() 



回答3:


Request Uri: getQueryParams()

Request Body: getBody()/getParsedBody()

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




回答4:


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.



来源:https://stackoverflow.com/questions/32668186/slim-3-how-to-get-all-get-put-post-variables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!