如何从 Symfony2 请求对象中删除单个参数 [英] How can I remove an individual parameter from a Symfony2 request object

查看:20
本文介绍了如何从 Symfony2 请求对象中删除单个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下请求对象,并希望在绑定到表单之前从控制器中删除email_suffix".这可能吗?

I have the following request object and would like to remove 'email_suffix' from a controller before binding to a form. Is this possible?

public 'request' => 
    object(Symfony\Component\HttpFoundation\ParameterBag)[8]
      protected 'parameters' => 
        array
          'registration' => 
            array
              'email' => string 's' (length=1)
              'email_suffix' => string 'y.com' (length=5)
              'password' => string '1234' (length=4)
              '_token' => string '967d99ba9f955aa67eb9eb004bd331151d816d06' (length=40)
          'product_id' => string '2' (length=1)
          'product_description' => string '12 month membership' (length=19)
          'product_price' => string '6.99' (length=4)

我试过 $request->request->remove("registration[email_suffix]");

我可以执行 $request->request->remove("registration") - 这行得通.

I can do $request->request->remove("registration") - this works.

现在,我正在这样做:

$requestReg = $request->request->get('registration');
$requestReg['email'] = $requestReg['email'].'@'.$requestReg['email_suffix'];
unset($requestReg['email_suffix']);
$request->request->set('registration',$requestReg);

推荐答案

我不确定,如果您的调用 $request->request 是错字.

I am not sure, if your call $request->request is typo.

你应该使用 $request->attributes 操作,它代表 ParameterBag 类.

You should operate with $request->attributes which represents ParameterBag class.

如果您将通过 中的方法ParameterBag 你会看到没有办法在数组内取消设置变量.

If you'll go through methods in ParameterBag you'll see that there is no way to unset variable inside array.

这篇关于如何从 Symfony2 请求对象中删除单个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆