Redirect to page with parameters in symfony

孤人 提交于 2019-12-25 03:14:01

问题


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);        
}

回答1:


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
));


来源:https://stackoverflow.com/questions/29499806/redirect-to-page-with-parameters-in-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!