我想在 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/