In Laravel, what is the relationship between request['name'] and request->get('name')?
曾经蜡笔没有小新2017-05-16 16:53:16
It doesn’t matter...if anything, they have one thing in common: they can all take parameter values.
Difference:
get()
是 Symfony Request 实现的,可以获取到 $request
的 attributes, query, request 里的值,并可以指定一个默认值。本意主要是提供给第三方框架取值或重写使用的。一般情况都是取到了 query 里的值,基本等同与 isset($_REQUEST['name']) ? $_REQUEST['name'] : $default
.
The array method is the method provided by Laravel to obtain the query parameter value. It is of course a little bit different for $request->all()
进行 data_get()
,支持 name 里的“点”分割获取数组的值,比如 $request['foo.bar']
, 性能上比直接 get()
, but it is very "Laravel way".
get()
方法在 Laravel 中对应的方法是 input()
和 file()
.
In addition to array methods, Laravel also provides many convenient methods to manipulate parameter values, such as exists
has
all
only
except
intersect
etc...
In Laravel, it works for $request
直接属性访问也可以取参数值,比如 $request->name
, but it is not recommended to use this method when it is unclear whether the name parameter exists