[php] Access POST values in Symfony2 request object

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!

This question is related to php symfony http-post

The answer is


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

$form->getClientData();


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.


Symfony doc to get request data

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

$content = $request->getContent();


what worked for me was using this:

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

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.


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.


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"];

Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to symfony

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? Composer require runs out of memory. PHP Fatal error: Allowed memory size of 1610612736 bytes exhausted SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) symfony2 The CSRF token is invalid. Please try to resubmit the form What is the difference between .yaml and .yml extension? How can I use break or continue within for loop in Twig template? Running Composer returns: "Could not open input file: composer.phar" Symfony2 and date_default_timezone_get() - It is not safe to rely on the system's timezone settings Symfony - generate url with parameter in controller Call PHP function from Twig template

Examples related to http-post

Passing headers with axios POST request How to post raw body data with curl? Send FormData with other field in AngularJS How do I POST a x-www-form-urlencoded request using Fetch? OkHttp Post Body as JSON What is the difference between PUT, POST and PATCH? HTTP Request in Swift with POST method Uploading file using POST request in Node.js Send POST request with JSON data using Volley AngularJS $http-post - convert binary to excel file and download