To get the details of the HTTP request you need to make use of the class IlluminateHttpRequest.
使用上面的类,您将能够从HTTP请求中获取输入、cookies和文件。现在考虑以下表单 -
To get all the details from the HTTP request you can do as follows −
Using $request->all() method
在下面的表单中输入以下详细信息:
Once you submit it will retrieve all the input data and return an array with data.
public function validateform(Request $request) { $input = $request->all(); print_r($input); }
The output of the above code is −
Array ( [_token] => 367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune )
Using $request->collect() method.
This method will return the data as a collection.
public function validateform(Request $request) { $input = $request->collect(); print_r($input); }
The output of the above code is −
Illuminate\Support\Collection Object ( [items:protected] => Array( [_token] => 367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune ) [escapeWhenCastingToString:protected] => )
使用 $request->getContent() 方法。
此方法将输出为URL查询字符串,数据以键/值对的形式传递。
public function validateform(Request $request) { $input = $request->getContent(); echo $input; }
The output of the above code is
_token=367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx&name=Rasika+Desai&email=rasika%40gmail.com&age=20&address=Pune
使用 php://input
这将返回来自URL查询字符串中输入字段的数据。
$data = file_get_contents('php://input'); print_r($data);
The output of the above code is −
_token=367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx&name=Rasika+Desai&email=rasika%40gmail.com&age=20&address=Pune
以上是在Laravel中如何获取HTTP请求体的内容?的详细内容。更多信息请关注PHP中文网其他相关文章!