访问 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没有返回任何东西。我做错了什么? 谢谢!

285291 次浏览

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:

$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.

I think that in order to get the request data, bound and validated by the form object, you must use :

$form->getClientData();

what worked for me was using this:

$data = $request->request->all();
$name = $data['form']['name'];

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.

Symfony 2.2

this solution is deprecated since 2.3 and will be removed in 3.0, see documentation

$form->getData();

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:

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:

$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.

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


Symfony 2.3

since 2.3 you should use handleRequest instead of bindRequest:

 $form->handleRequest($request);

see documentation

There is one trick with ParameterBag::get() method. You can set $deep parameter to true and access the required deep nested value without extra variable:

$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.

The field data can be accessed in a controller with: Listing 12-34

$form->get('dueDate')->getData();

In addition, the data of an unmapped field can also be modified directly: Listing 12-35

$form->get('dueDate')->setData(new \DateTime());

page 164 symfony2 book(generated on October 9, 2013)

I access the ticketNumber parameter for my multipart post request in the following way.

$data = $request->request->all();
$ticketNumber = $data["ticketNumber"];

Symfony doc to get request data

Finally, the raw data sent with the request body can be accessed using getContent():

$content = $request->getContent();