在 Laravel 中,如何获得 *only* POST 参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27366243/
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
In Laravel, how can I get *only* POST parameters?
提问by NeuronQ
I know that one can use $request->get('my_param')or Input::get('my_param')to get a POST or GET request parameter in Laravel (I'm toying with v5/dev version now, but it's the same for 4.2).
我知道可以在 Laravel 中使用$request->get('my_param')或Input::get('my_param')获取 POST 或 GET 请求参数(我现在正在使用 v5/dev 版本,但对于 4.2 来说也是如此)。
But how can I make sure that my my_paramcame via a POST parameter and was not just from a ?my_param=42appended to the URL?(besides reverting to the ol' $_POSTand $_GETsuperglobals and throwing testability out the window)
但是我如何确保我my_param是通过 POST 参数来的,而不仅仅是来自?my_param=42附加到 URL 的参数?(除了恢复到 ol'$_POST和$_GETsuperglobals 并将可测试性扔出窗外)
(Note: I also know that the Request::getmethod will give me the POST param for a POST request, if both a POST an URL/GET param with the same name exist, but... but if the param land in via the url query string instead, I want a Laravel-idiomatic way to know this)
(注意:我也知道该Request::get方法将为我提供 POST 请求的 POST 参数,如果 POST 和 URL/GET 参数存在相同的名称,但是...但是如果参数通过 url 查询字符串进入相反,我想要一种 Laravel 惯用的方式来了解这一点)
采纳答案by lukasgeiter
In the class Illuminate\Http\Request(or actually the Symphony class it extends from Symfony\Component\HttpFoundation\Request) there are two class variables that store request parameters.
在类Illuminate\Http\Request(或者实际上是从它扩展的 Symphony 类Symfony\Component\HttpFoundation\Request)中有两个存储请求参数的类变量。
public $query- for GET parameters
public $query- 对于 GET 参数
public $request- for POST parameters
public $request- 用于 POST 参数
Both are an instance of Symfony\Component\HttpFoundation\ParameterBagwhich implements a getmethod.
两者都是Symfony\Component\HttpFoundation\ParameterBag实现get方法的实例。
Here's what you can do (although it's not very pretty)
这是你可以做的(虽然它不是很漂亮)
$request = Request::instance();
$request->request->get('my_param');
回答by mokrane2203
Why trying to complicate things when you can do easily what you need :
当您可以轻松完成您需要的事情时,为什么还要尝试使事情复杂化:
$posted = $_POST;

