Symfony2:如果变量post存在,我如何得到


Symfony2 : how i get if variable post exist

我将尝试验证是否存在一个变量POST,但我的函数无法识别。

$request = $this->get('request');
if($request->has('variable')){
// do something
}

但是函数没有被实现到请求中,但我看到的是被实现到parameterBag中。所以我不知道如何使用parameterBag。

我已经尝试在谷歌搜索,但所有的解决方案都是使用

"$request->has('variable')"
or
$request->hasParameter('variable')

但是has和hasParameter不是请求中的函数。。。

感谢您的帮助,

问候,

Jérôme

POST变量存储在$request->request中(命名令人困惑)。试试这个,

if ($request->request->has('variable')) {
    //do something
}

请记住,请求的每个属性都是ParameterBag实例(或的子类)。这里的重点是针对request属性(保存POST数据)

我可以补充一点,如果您希望在Controller中执行此操作,则可以不使用"request"服务,而是添加$request对象作为操作的参数(因为它将自动替换为实际的request对象)。