-1

I'm trying to make a redirect but with post, to avoid all the parameters in the Url. I'm using Symfony 4.3 and PHP 7.2

Here's my code:

return $this->redirect($this->generateUrl('my-account', $arrayOfValues));

When I do this, GET is what is used. There's a way to make it fly via POST? Thanks

benair3
  • 3
  • 2

2 Answers2

-1

It's impossible to redirect a POST request because the browser would have to re-send the POST data (which it doesn't). What you should do instead in this case is use forwarding.

Patrick Simard
  • 2,294
  • 3
  • 24
  • 38
-1

You would be better off putting the array in a session and then redirecting without GET parameters and then accessing the session variables in the next page.

You can POST with JavaScript, though this is from years ago and there is undoubtedly a better way now:

function http_post_redirect($url='', $data=array(), $doc=true) {

    $data = json_encode($data);

    if($doc) { echo "<html><head></head><body>"; }

    echo "
    <script type='text/javascript'>
        var data = eval('(' + '$data' + ')');
        var jsForm = document.createElement('form');

        jsForm.method = 'post';
        jsForm.action = '$url';

        for (var name in data) {
            var jsInput = document.createElement('input');
            jsInput.setAttribute('type', 'hidden');
            jsInput.setAttribute('name', name);
            jsInput.setAttribute('value', data[name]);
            jsForm.appendChild(jsInput);
        }
        document.body.appendChild(jsForm);
        jsForm.submit();
    </script>";

    if($doc) { echo "</body></html>"; }
    exit;
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87