访问 Symfony2 请求对象中的 POST 值
好吧,这是一个新手问题,但我在任何地方都找不到答案。在 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');
不幸的是 $name_value
没有返回任何内容。我做错了什么?谢谢!
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:
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
表单发布值存储在请求中表单的名称下。例如,如果您已重写 ContactType() 的
getName()
方法以返回“contact”,则可以执行以下操作:如果仍然遇到问题,请尝试执行
var_dump
查看所有发布值。$request->request->all()
上的 ()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:If you're still having trouble, try doing a
var_dump()
on$request->request->all()
to see all the post values.Symfony 2.2
此解决方案自 2.3 起已弃用,并将在 3.0 中删除,查看文档
表单参数的数组
为您提供来自 symfony2 book 第 162 页(第 12 章:表格)
[...] 有时,您可能只想使用没有类的表单,并返回提交的数组
数据。这实际上非常简单:
您还可以直接通过请求对象访问 POST 值(在本例中为“name”),如下所示:
但是,请注意,在大多数情况下使用 getData() 方法更好选择,因为它
返回被表单框架转换后的数据(通常是一个对象)。
当你想访问表单令牌时,你必须使用Problematic的答案
$postData = $request->request->get('contact');
因为getData()
从数组Symfony 2.3< /strong>
从 2.3 开始,您应该使用
handleRequest
而不是bindRequest
:查看文档
Symfony 2.2
this solution is deprecated since 2.3 and will be removed in 3.0, see documentation
gives you an array for the form parameters
from symfony2 book page 162 (Chapter 12: Forms)
[...] 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:
You can also access POST values (in this case "name") directly through the request object, like so:
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.
When you want to access the form token, you have to use the answer of Problematic
$postData = $request->request->get('contact');
because thegetData()
removes the element from the arraySymfony 2.3
since 2.3 you should use
handleRequest
instead ofbindRequest
:see documentation
对我有用的是使用这个:
what worked for me was using this:
ParameterBag::get()
方法有一个技巧。您可以设置$deep< /code> 参数
为
true
并访问所需的深层嵌套值,无需额外变量:您还可以设置默认值(第二个参数
get()
方法),可以避免冗余的isset($form['some']['deep']['data'])
调用。There is one trick with
ParameterBag::get()
method. You can set$deep
parameter totrue
and access the required deep nested value without extra variable:Also you have possibility to set a default value (2nd parameter of
get()
method), it can avoid redundantisset($form['some']['deep']['data'])
call.可以通过以下方式在控制器中访问现场数据:
示例12-34
另外,未映射字段的数据也可以直接修改:
清单12-35
第164页symfony2书(2013年10月9日生成)
The field data can be accessed in a controller with:
Listing 12-34
In addition, the data of an unmapped field can also be modified directly:
Listing 12-35
page 164 symfony2 book(generated on October 9, 2013)
我通过以下方式访问多部分发布请求的 TicketNumber 参数。
I access the ticketNumber parameter for my multipart post request in the following way.
我认为为了获取由表单对象绑定和验证的请求数据,您必须使用:
$form->getClientData();
I think that in order to get the request data, bound and validated by the form object, you must use :
$form->getClientData();
Symfony doc 获取请求数据
$content = $request->getContent();
Symfony doc to get request data
$content = $request->getContent();
如果您是新手,欢迎来到 Symfony2,这是一个开源项目,所以如果您想学习很多东西,您可以开源!
来自“Form.php”:
getData()
获取标准数据()
getViewData()
您可以在此文件中找到更多详细信息。
If you are newbie, welcome to Symfony2, an open-source project so if you want to learn a lot, you can open the source !
From "Form.php" :
getData()
getNormData()
getViewData()
You can find more details in this file.