How to get POST global variable in PHP Symfony 4?
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:
- http://php.net/manual/en/reserved.variables.post.php
- https://symfony.com/doc/current/introduction/http_fundamentals.html#symfony-request-object
Solution 4
The possible solution could be:
$post_data = json_decode($request->getContent(), true);
$date = $post_data['date'];
Alexandre Martin
Updated on July 09, 2022Comments
-
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 :
But $date is
null
. -
Alexandre Martin about 5 yearsAs 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 about 5 yearsThe 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 about 5 yearsThere is no valuable solutions, I still have the problem
-
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 over 4 years$request->get('parameter') will allow you just to get the get variables.
-
yivi over 4 yearsI do not understand what you mean, @Taou. Something I should correct in my answer?
-
TaouBen over 4 yearsI 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 over 4 years@TaouBen
Request::get()
works both forGET
andPOST
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).