1

I'd like to send at the same time some parameters in the URL (with redirectToRoute) and some not in the URL (with render). How can I do ?

To show an example : I have two var : A and B

A needs to be in the URL : http://website.com?A=smth B needs to be send to complete the TWIG (but not by URL)

Can you show me an example of code to do it ?

Thanks

Pierre
  • 490
  • 1
  • 7
  • 26
  • or use this approach https://stackoverflow.com/questions/11227975/symfony-2-redirect-using-post/31031986#31031986 – LBA Sep 25 '17 at 13:42

2 Answers2

2

A HTTP 3xx redirect does NOT have a body so you can't include data via render and use redirectToRoute('redirect_target_route', array('A' => 'smth'}) at the same time.

You'd need to save the data in a session flashbag and get it from there inside the controller action for redirect_target_route.

public function redirectingAction(Request $request)
{
    // ...
    // store the variable in the flashbag named 'parameters' with key 'B'
    $request->getSession()->getFlashBag('parameters')->add('B', 'smth_else');

    // redirect to uri?A=smth
    return $this->redirectToRoute('redirect_target_route', array('A' => 'smth'});
}

public function redirectTargetAction(Request $request)
{
    $parameterB = $request->getSession()->getFlashBag('parameters')->get('B');
    // ...
}
Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
0

Nice and easy, just pass an array of keys/values into the render() method:

$template = $twig->load('index.html');
echo $template->render(array('the' => 'variables', 'go' => 'here'));

https://twig.symfony.com/doc/2.x/api.html#rendering-templates

delboy1978uk
  • 12,118
  • 2
  • 21
  • 39
  • I don't think @Destunk wants to pass variables to render but store/retrieve them across a redirect as `redirectToRoute` is mentioned in the question. – Nicolai Fröhlich Sep 25 '17 at 13:53