php 访问 Symfony2 请求对象中的 POST 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6916324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Access POST values in Symfony2 request object
提问by Acyra
OK, this is a newbie question, but I can't find the answer anywhere. In a controller in Symfony2, I want to access the POST value from one of my forms. In the controller I have:
好的,这是一个新手问题,但我在任何地方都找不到答案。在 Symfony2 的控制器中,我想从我的表单之一访问 POST 值。在控制器中,我有:
public function indexAction()
{
$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form = $this->get('form.factory')->create(new ContactType());
$form->bindRequest($request);
if ($form->isValid()) {
$name_value = $request->request->get('name');
Unfortunately $name_value
isn't returning anything. What am I doing wrong? Thanks!
不幸的$name_value
是没有返回任何东西。我究竟做错了什么?谢谢!
回答by Problematic
The form post values are stored under the name of the form in the request. For example, if you've overridden the getName()
method of ContactType() to return "contact", you would do this:
表单发布值存储在请求中表单的名称下。例如,如果您覆盖了getName()
ContactType()的方法以返回“contact”,您将执行以下操作:
$postData = $request->request->get('contact');
$name_value = $postData['name'];
If you're still having trouble, try doing a var_dump()
on $request->request->all()
to see all the post values.
如果您还是有问题,尝试做一个var_dump()
上$request->request->all()
看到所有的提交值。
回答by timaschew
Symfony 2.2
Symfony 2.2
this solution is deprecated since 2.3 and will be removed in 3.0, see documentation
此解决方案自 2.3 起已弃用,并将在 3.0 中删除,请参阅文档
$form->getData();
gives you an array for the form parameters
给你一个表单参数数组
from symfony2 bookpage 162 (Chapter 12: Forms)
来自symfony2 书第 162 页(第 12 章:表格)
[...] sometimes, you may just want to use a form without a class, and get back an array of the submitted data. This is actually really easy:
[...] 有时,您可能只想使用没有类的表单,并获取提交数据的数组。这实际上非常简单:
public function contactAction(Request $request) {
$defaultData = array('message' => 'Type your message here');
$form = $this->createFormBuilder($defaultData)
->add('name', 'text')
->add('email', 'email')
->add('message', 'textarea')
->getForm();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
// data is an array with "name", "email", and "message" keys
$data = $form->getData();
}
// ... render the form
}
You can also access POST values (in this case "name") directly through the request object, like so:
您还可以直接通过请求对象访问 POST 值(在本例中为“名称”),如下所示:
$this->get('request')->request->get('name');
Be advised, however, that in most cases using the getData() method is a better choice, since it returns the data (usually an object) after it's been transformed by the form framework.
但是,请注意,在大多数情况下,使用 getData() 方法是更好的选择,因为它在被表单框架转换后返回数据(通常是一个对象)。
When you want to access the form token, you have to use the answer of Problematic
$postData = $request->request->get('contact');
because the getData()
removes the element from the array
当您想访问表单令牌时,您必须使用问题的答案,
$postData = $request->request->get('contact');
因为getData()
从数组中删除元素
Symfony 2.3
Symfony 2.3
since 2.3 you should use handleRequest
instead of bindRequest
:
从 2.3 开始,您应该使用handleRequest
代替bindRequest
:
$form->handleRequest($request);
回答by ramzey
what worked for me was using this:
对我有用的是使用这个:
$data = $request->request->all();
$name = $data['form']['name'];
回答by Dmitriy
There is one trick with ParameterBag::get()
method. You can set $deep
parameterto true
and access the required deep nested value without extra variable:
方法有一个技巧ParameterBag::get()
。您可以将$deep
参数设置为true
并访问所需的深度嵌套值,而无需额外的变量:
$request->request->get('form[some][deep][data]', null, true);
Also you have possibility to set a default value (2nd parameter of get()
method), it can avoid redundant isset($form['some']['deep']['data'])
call.
您也可以设置默认值(get()
方法的第二个参数),它可以避免冗余isset($form['some']['deep']['data'])
调用。
回答by meteor
The field data can be accessed in a controller with: Listing 12-34
可以在控制器中访问字段数据:清单 12-34
$form->get('dueDate')->getData();
In addition, the data of an unmapped field can also be modified directly: Listing 12-35
另外,未映射字段的数据也可以直接修改:示例12-35
$form->get('dueDate')->setData(new \DateTime());
page 164 symfony2 book(generated on October 9, 2013)
第 164 页 symfony2 书(生成于 2013 年 10 月 9 日)
回答by Ilker Baltaci
I access the ticketNumber parameter for my multipart post request in the following way.
我通过以下方式访问我的多部分发布请求的 ticketNumber 参数。
$data = $request->request->all();
$ticketNumber = $data["ticketNumber"];
回答by Chicna
I think that in order to get the request data, bound and validated by the form object, you must use :
我认为为了获取由表单对象绑定和验证的请求数据,您必须使用:
$form->getClientData();
$form->getClientData();
回答by Alex Joe
回答by Thomas Decaux
If you are newbie, welcome to Symfony2, an open-source project so if you want to learn a lot, you can open the source !
如果你是新手,欢迎来到 Symfony2,一个开源项目,所以如果你想学到很多东西,你可以开源!
From "Form.php" :
来自“Form.php”:
getData() getNormData() getViewData()
getData() getNormData() getViewData()
You can find more details in this file.
您可以在此文件中找到更多详细信息。