laravel的$request->input()和$request->get()有什么区别?

laravel的$request->input()和$request->get()有什么区别?

 public function store(Request $request)
{
    $name=$request->input('name');
    $email=$request->get('email');
}
阅读 16.3k
3 个回答

走的流程不同
input获取数据的流程是把post过来的数据与URL里的Query合并,然后用helper里的data_get方法去取数据

/**
 * 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 data_get($input, $key, $default);
}

get方法用的是SymfonyComponentHttpFoundation里的get

/**
 * Gets a "parameter" value from any bag.
 *
 * This method is mainly useful for libraries that want to provide some flexibility. If you don't need the
 * flexibility in controllers, it is better to explicitly get request parameters from the appropriate
 * public property instead (attributes, query, request).
 *
 * Order of precedence: PATH (routing placeholders or custom attributes), GET, BODY
 *
 * @param string $key     the key
 * @param mixed  $default the default value if the parameter key does not exist
 *
 * @return mixed
 */
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 5.4 Request

除了获取http parameter 的raw data 以外,上面文档里Request Input 提供的数据获取方式,SymfonyHttpFoundation Request 均未提供

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏