讨论数量:
两个都可以获取用户输入的数据,不同的是, get()
方法是 Symfony\Component\HttpFoundation\Request
提供的一个方法,他的源码是这样的。
public function get($key, $default = null)
{
if ($this !== $result = $this->attributes->get($key, $this)) {
return $result;
}
if ($this !== $result = $this->query->get($key, $this)) {
return $result;
}
if ($this !== $result = $this->request->get($key, $this)) {
return $result;
}
return $default;
}
在Laravel中是不推荐使用的,Laravel中推荐使用的是Laravel自己的方法 input()
,
input()
方法是 Laravel
框架自带的方法, 在 Illuminate\Http\Request
里, Request
继承的是Symfony\Component\HttpFoundation\Request
/**
* Retrieve an input item from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function input($key = null, $default = null)
{
$input = $this->getInputSource()->all() + $this->query->all();
return Arr::get($input, $key, $default);
}
而 get()
方法则在 Symfony
框架中, 在 Symfony\Component\HttpFoundation\Request
里
/**
* Gets a "parameter" value.
*
* This method is mainly useful for libraries that want to provide some flexibility.
*
* Order of precedence: GET, PATH, POST
*
* Avoid using this method in controllers:
*
* * slow
* * prefer to get from a "named" source
*
* It is better to explicitly get request parameters from the appropriate
* public property instead (query, attributes, request).
*
* @param string $key the key
* @param mixed $default the default value if the parameter key does not exist
* @param bool $deep is parameter deep in multidimensional array
*
* @return mixed
*/
public function get($key, $default = null, $deep = false)
{
if ($this !== $result = $this->query->get($key, $this, $deep)) {
return $result;
}
if ($this !== $result = $this->attributes->get($key, $this, $deep)) {
return $result;
}
if ($this !== $result = $this->request->get($key, $this, $deep)) {
return $result;
}
return $default;
}
两者都是获取请求参数的值
推荐文章: