To get the details of the HTTP request you need to make use of the class Illuminate\Http\Request.
使用上面的類,您將能夠從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中文網其他相關文章!