symfony - 向 symfony2 请求深度数组添加参数

标签 symfony request symfony-2.0

我想在 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 数组 添加一个字段。我试过了

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

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

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

最佳答案

如果 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

关于symfony - 向 symfony2 请求深度数组添加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14214428/

相关文章:

symfony - 未找到类 'Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension'

symfony - Symfony:找不到命令

java - 在 Android 中进行多部分文件上传的好方法

python - 使用python requests模块获取验证码图像的问题

php - Symfony 2 原型(prototype)内的原型(prototype) - 双 $$name 字段

php - Symfony2 在路由中使用默认语言环境(一种语言的一个 URL)

PHP/MySQL 外键以 id=0 的形式保存到数据库

unit-testing - 使用 reCaptcha 测试表单提交的安全和标准方法是什么?

node.js - 在继续主函数之前如何获取回调函数的结果?