'How to get POST global variable in PHP Symfony 4?

I have a very strange problem getting the result of a POST global variable in Symfony 4.

I tried this way:

$date = $request->request->get('date');

This is how I actually send the AJAX request when the Calendar input's date changed:

onSelect: function(date, instance) {
    $.ajax({
      url : 'home',
      type : 'POST',
      data : {'date':date},
      dataType : 'html',
      success : function(code_html, statut){
        console.log(statut);
      },

      error : function(resultat, statut, erreur){
      
      },

      complete : function(resultat, statut){

      }          
    });

The onSelect callback successfully receive the date value I want.

And this result shows the 200 success code with right values for the date variable :

profiler screenshot

But $date is null.



Solution 1:[1]

There is no problem with headers or anything like that in your code, and the jQuery request is correct as it is (I tested your code).

The problem might be with your PHP code, which is not present in your question.

If you are injecting the Request object correctly, you can easily use get() to retrieve the post variables.

For example:

namespace App\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class VeryDefaultController extends AbstractController
{

   /**
   * @Route("/very/default", name="very_default")
   * @param Request $request
   *
   * @return \Symfony\Component\HttpFoundation\Response
   */
   public function index(Request $request)
   { 
       $date = $request->get('date');
   }
}

In your example you are doing $request->request->get('date') which is fine for a POST or PUT request, but it's probably failing because you are not injecting the Request object correctly.

The above code is tested with your jQuery AJAX POST request, and it works. There is nothing inherently wrong in that part. And it is perfectly possible to access POST variables using get().

Solution 2:[2]

you are sending a JSON string, hence you'll need to use:

$content = $request->getContent();

and then you'll need to parse it with json_decode.

Solution 3:[3]

The problem you may face is related with Accept HTML header that PHP uses to convert the variables from post body to $_POST and Symfony uses $_POST to fill the request object data.

To get this behaviour you need to use header application/x-www-form-urlencoded or multipart/form-data.

To check if that's actually the case you need to use the code:

dump($request->getContent());

If it's filled with variables then it means PHP doesn't convert them to $_POST because of MIME mismatch, if not then it means that request is incorrect.

Normal approach to access post variable is:

public function index(Request $request) 
{
    dump($request->getContent()); // get raw body of http request 
    dump($request->request->get('date')); //get converted variable date by php and then by symfony
}

Useful links to check:

Solution 4:[4]

The possible solution could be:

$post_data = json_decode($request->getContent(), true);
$date = $post_data['date'];

Solution 5:[5]

Try adding the content type, stringify your json data and use traditional serialization, like this:

onSelect: function(date, instance) {
var data = {'date':date};
$.ajax({
  url : 'home',
  type : 'POST',
  data : JSON.stringify(data),
  contentType: "application/json; charset=utf-8",
  traditional: true,
  dataType : 'html',
  success : function(code_html, statut){
    console.log(statut);
  },

  error : function(resultat, statut, erreur){

  },

  complete : function(resultat, statut){

  }          
});

As you are not posting your controller code I suggest being sure that:

  • You have a use statement like \Symfony\Component\HttpFoundation\Request;
  • You are injecting the Request like public function post( Request $request )
  • You are reading your data like $data = json_decode( $request->getContent(), true);. On this example you will have the data as an assoc array.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 crmpicco
Solution 2 yivi
Solution 3 yivi
Solution 4
Solution 5 user9342572809