Search code examples
phpsymfonysymfony-forms

Symfony Forms manual submission of both GET and POST requests


Is there any way to get merged data from Request class? Because currently we are submitting forms manually for API controller that accepts both POST and GET queries (this is not an REST API because of legacy project).

$data = array_merge($request->query->all(), $request->request->all());
$form->submit($data);

Is there any way to write something cleaner instead of code below?

$data = array_merge($request->query->all(), $request->request->all());

Solution

  • I think that is not possible. (Maybe I'm wrong)

    If you look at the source code of Request, you can see that when Symfony create the request, Symfony put the global variable $_GET in $this->query and $_POST in $this->request.
    There is no Symfony variable that takes both.

    If you need it in only one place, I think what you have done is fine.

    If not, create a service that will or another solution that factors this code.

    Another solution is to use the global variable $_REQUEST, because Symfony makes the merge, but it depends on your php configuration (request_order parameter of your php.ini).
    But I do not think using Superglobals variables with Symfony is a good idea ... (Besides symfony overwrites them)