Mateusz
Mateusz

Reputation: 331

Redirect to page with parameters in symfony

So I'm on page which looks like:

project2/web/calculator

And when I click submit in form I want to redirect to address like this one:

project2/web/result?weight=80&height=190

How I can do this? I know only how can I show these parameters:

if ($form->isValid())
{
    $weight = $form->get('weight')->getData();
    $height = $form->get('height')->getData();

    return new Response('Test: ' . $weight . ' ' . $height);        
}

Upvotes: 0

Views: 6760

Answers (1)

MikO
MikO

Reputation: 18751

Assuming you have defined a route named result that matches the URL project2/web/result, and assuming you are in a Controller, then you can redirect to that route with the query parameters you want - something like:

return $this->redirect($this->generateUrl('result', array(
    'height' => $height,
    'weight' => $weight
));

That said, it looks to me like you're trying to do something weird - why do you need to redirect to another route? I'd need more details, but in a normal Symfony2 scenario, you'd probably want to render a Twig template passing those values with:

return $this->render('result_view', array(
    'height' => $height,
    'weight' => $weight
));

Upvotes: 1

Related Questions