How to get POST global variable in PHP Symfony 4?

22,668

Solution 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

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

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

The possible solution could be:

$post_data = json_decode($request->getContent(), true);
$date = $post_data['date'];
Share:
22,668
Alexandre Martin
Author by

Alexandre Martin

Updated on July 09, 2022

Comments

  • Alexandre Martin
    Alexandre Martin almost 2 years

    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.

  • Alexandre Martin
    Alexandre Martin about 5 years
    As the profiler shows the post variable and the success code, I think the PHP part is correct. POST variable is sent, but I can't get it.
  • yivi
    yivi about 5 years
    The POST variable is sent, and you can get it. It's simply you are not getting it correctly. In the only code you show, you do $request->request->get(), which doesn't appear to make a lot of sense. The above code is tested, and it works.
  • Alexandre Martin
    Alexandre Martin about 5 years
    There is no valuable solutions, I still have the problem
  • yivi
    yivi about 5 years
    @AlexandreMartin then something is missing from your question. With your jquery code and the code from my answer, it works. If you don’t provide the missing pieces, nobody can help you.
  • TaouBen
    TaouBen over 4 years
    $request->get('parameter') will allow you just to get the get variables.
  • yivi
    yivi over 4 years
    I do not understand what you mean, @Taou. Something I should correct in my answer?
  • TaouBen
    TaouBen over 4 years
    I think so, your answer did not xork for me, if my variable is a get variable then it works, but post variables worked for me by using getContent() method and then parsing the response with json decode, I am using symfony 4.
  • yivi
    yivi over 4 years
    @TaouBen Request::get() works both for GET and POST parameters. What you are describing is getting the request body; which is not what this question was trying to do. (Again, I tested the code with their specific AJAX request).