Pierre
Pierre

Reputation: 510

Symfony - Send parameters with and without the URL at the same time

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

Upvotes: 1

Views: 1964

Answers (2)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52483

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');
    // ...
}

Upvotes: 2

delboy1978uk
delboy1978uk

Reputation: 12365

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

Upvotes: 0

Related Questions