In Laravel, performance comparison of request['name'] and request->get('name')
我想大声告诉你
我想大声告诉你 2017-05-16 16:51:16
0
1
514

In Laravel, what is the relationship between request['name'] and request->get('name')?

我想大声告诉你
我想大声告诉你

reply all(1)
曾经蜡笔没有小新

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template