【问题标题】:Add Parameter to symfony2 Request deep array向 symfony2 请求深度数组添加参数
【发布时间】:2012-12-22 06:33:11
【问题描述】:

我想在 symfony2 请求中向 ParameterBag 添加一个参数。

所以数组看起来像这样:

array (size=2)
  'editor' => 
    array (size=6)
      '_token' => string '5797a4faf1fced89404b80fb04b3cadffc99695e' (length=40)
      'login' => string 'editor' (length=6)
      'firstName' => string 'Joh' (length=3)
      'lastName' => string 'Ha' (length=2)
      'address' => 
        array (size=6)
          'time_zone_code' => string 'Africa/Abidjan' (length=14)

我想在editor array 中添加一个字段。我试过这个

$request->request->add(array('editor[password]' => $password));

当然,这只是在editor array 之后添加一个名为editor[password] 的字段。

我是否必须替换整个 ParameterBag 或者是否有添加值的方法?

【问题讨论】:

    标签: symfony request symfony-2.0


    【解决方案1】:

    如果 editor 不是 ParameterBag 中唯一的数组,您可以获取 editor 数组,然后为其添加一个值并再次设置它:

    $data = $this->getRequest()->request->get('editor');
    $data['password'] = 'string';
    $this->getRequest()->request->set('editor', $data);
    

    编辑

    Google 群组中似乎有一个问题与类似的答案相似:https://groups.google.com/forum/?fromgroups=#!topic/symfony-devs/2-SWFtFKwxQ

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-18
      • 2016-04-13
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多