0

My controller has just finished dealing with a form, and

if ($form->isValid())
{
  // Persist my data.
  // ...

  // Now move on to the next page.
  return $this->redirectToRoute('page2');
}

But I would like to pass variable to that page using POST. I.e. something similar to:

$this->redirectToRoute('page2', array('state' => 'wonderful'));

This will obviously pass it as a GET parameter. People have suggested using redirect 307s and forwarding, but they just forward the current request along. What I need to do is create a new Request with my arbitrary message. How can I accomplish this?

Community
  • 1
  • 1
Druckles
  • 3,161
  • 2
  • 41
  • 65

4 Answers4

0

Not sure if this will work, but worth a try:

// Add here whatever you need to the request
$request->initialize(
   $request->query->all(),
   $request->request->all(),
   $request->attributes->all(),
   $request->cookies->all(),
   $request->files->all(),
   $request->server->all(),
   $request->getContent());

return $this->redirectToRoute('route', [
        'request' => $request
    ], 307);
gvf
  • 1,039
  • 7
  • 6
0

Delegate your redirect to client side and there make post request.

your controller:

if ($form->isValid())
{
  return $this->render('redirect.html.twig');
}

redirect.html.twig:

<html>
<body onload="document.frm1.submit()">
    <form action="{{path('page2')}}" method="POST" name="frm1">
        <input type="hidden" name="state" value="wonderful" />
    </form>
</body>
</html>

Btw you can bind post parameters to Symfony form and render it then in template.

karser
  • 1,625
  • 2
  • 20
  • 24
0

You can use session

In initial controller

$this->get('session')->set('request',$request)

In other

$request=$this->get('session')->get('request')

Or you can use cookies

$response->headers->setCookie(new Cookie('request', $request));

->

$response->headers->getCookie('request'));
cesarve
  • 274
  • 3
  • 9
0

You can't simulate POST directly.
If you must send with POST you have to use:

Check or ask about you're form.
I find it strange to have to send POST data in another route in your project.

Roukmoute
  • 681
  • 1
  • 11
  • 26