laravel 将 Illuminate\Http\Request 转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32586627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Illuminate\Http\Request to array
提问by Gilles Major
I'm making in use of a library that expects to receive a native PHP array such as is obtained from the global $_REQUEST.
我正在使用一个库,该库希望接收一个本地 PHP 数组,例如从全局 $_REQUEST 获得的数组。
Unfortunately in this instance, the Illuminate\Http\Request is an object, and seems to have quite a different structure to the native php array. Is there a method to convert that object to the same array that would be obtained via '$_REQUEST' (which doesn't work inside a laravel controller method).
不幸的是,在这种情况下, Illuminate\Http\Request 是一个对象,并且似乎与本机 php 数组具有完全不同的结构。是否有一种方法可以将该对象转换为通过“$_REQUEST”获得的相同数组(这在 Laravel 控制器方法中不起作用)。
Thanks in advance.
提前致谢。
回答by Chris Magnussen
Illuminate\Http\Request has some nice methods to turn it into arrays.
Illuminate\Http\Request 有一些很好的方法可以把它变成数组。
// Where $request is an Illuminate\Http\Request instance
$request->all(); // Returns array with all elements.
$request->only(['key1', 'key2']); // Returns array with selected items
$request->except(['key1']); // Returns array with everything except key1.
You should always check the class for methods you can use. Just browse through the files and see what they have to offer. It's fun and gives you a better understanding of the api.
您应该始终检查类以获取您可以使用的方法。只需浏览文件,看看它们提供了什么。这很有趣,可以让您更好地了解 api。
Laravel really shines with its extensive documentation, make sure you take advantage of it.
Laravel 以其丰富的文档而大放异彩,确保您充分利用它。
回答by Max
You can cast to array: $request_arr = (array) $_REQUEST;
您可以转换为数组: $request_arr = (array) $_REQUEST;