Laravel 5 只从请求中获取 GET 或 POST 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31585716/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:01:05  来源:igfitidea点击:

Laravel 5 get only GET or POST params from request

phplaravelrequestlaravel-5

提问by MaGnetas

I can access request params using Request::input()or Request::all().

我可以使用Request::input()或访问请求参数Request::all()

The problem is that my request includes both GET and POST params, but only GET ones are used to calculate signature.

问题是我的请求同时包含 GET 和 POST 参数,但只有 GET 参数用于计算签名。

Is there a way to retrieve only a set of GET or a set of POST params from request in Laravel 5.1?

有没有办法只从 Laravel 5.1 中的请求中检索一组 GET 或一组 POST 参数?

Or going with $_GET and $_POST is my only option here?

或者使用 $_GET 和 $_POST 是我唯一的选择?

Thank you.

谢谢你。

采纳答案by jedrzej.kurylo

You can use Request::query()to get only GETparameters. Keep in mind that there are no guaranties about consistency in the order of parameters you get from GET, so you might need to sort the array before calculating the signature - depending on how you calculate the signature.

您可以使用Request::query()获取 GET参数。请记住,对于从 GET 获得的参数顺序的一致性没有任何保证,因此您可能需要在计算签名之前对数组进行排序 - 取决于您计算签名的方式。

回答by aamuller

If you need something straightforward you can just use the global helper:

如果你需要一些简单的东西,你可以使用全局助手:

$pathData = request()->path(); <br />
$queryData = request()->query(); <br />
$postData = array_diff(request()->all(), request()->query());

https://laravel.com/docs/5.6/requests

https://laravel.com/docs/5.6/requests

回答by Tobia

Follow these instructions to extend the Laravel Request class with your own:

按照以下说明使用您自己的扩展 Laravel 请求类:

https://stackoverflow.com/a/30840179/517371

https://stackoverflow.com/a/30840179/517371

Then, in your own Request class, copy the input()method from Illuminate\Http\Requestand remove + $this->query->all():

然后,在您自己的 Request 类中,复制input()方法 fromIlluminate\Http\Request并删除+ $this->query->all()

public function input($key = null, $default = null)
{
    $input = $this->getInputSource()->all();

    return data_get($input, $key, $default);
}

Bingo! Now in a POST request, Request::query()returns the query (URL) parameters, while Request::input()only returns parameters from the form / multipart / JSON / whatever input source.

答对了!现在在 POST 请求中,Request::query()返回查询(URL)参数,而Request::input()仅从表单/多部分/JSON/任何输入源返回参数。