在Laravel中,我怎样才能获得* * * POST参数? [英] In Laravel, how can I get *only* POST parameters?
问题描述
我知道可以使用 $ request-> get('my_param')
或输入:: get('my_param')
在Laravel中获取POST或GET请求参数(我现在正在使用v5 / dev版本,但对于4.2来说它是相同的。)
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).
但是如何通过POST参数确保我的 my_param
来自?my_param = 42
附加到网址?(除了还原为ol' $ _ POST
和 $ _ GET
超级全局和抛出可测试性窗外)
But how can I make sure that my my_param
came via a POST parameter and was not just from a ?my_param=42
appended to the URL? (besides reverting to the ol' $_POST
and $_GET
superglobals and throwing testability out the window)
(注意:我也知道 Request :: get
方法会给我一个POST请求的POST参数,如果POST和一个同名的URL / GET参数都存在,但是......但如果参数通过url查询字符串登陆,我想要一个Laravel惯用的方式要知道这个)
(Note: I also know that the Request::get
method 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)
推荐答案
在课堂上 Illuminate\Http\Request
(或实际上它扩展的Symphony类来自 Symfony\Component\HttpFoundation\Request
)有两个存储请求参数的类变量。
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.
public $ query
- 用于GET参数
public $query
- for GET parameters
public $ request
- 对于POST参数
public $request
- for POST parameters
两者都是 Symfony \Component \ HttpFoundation \ParameterBag
的实例一个获取
方法。
Both are an instance of Symfony\Component\HttpFoundation\ParameterBag
which implements a get
method.
这是你可以做的(尽管它不是很漂亮)
Here's what you can do (although it's not very pretty)
$request = Request::instance();
$request->request->get('my_param');
这篇关于在Laravel中,我怎样才能获得* * * POST参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!