使用symfony中的参数重定向到页面 [英] 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);
}
推荐答案
假设您定义了一个与URL project2/web/result
相匹配的名为result
的路由,并假设您位于Controller
中,则可以重定向至具有所需查询参数的路由-类似于:
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
));
也就是说,在我看来,您正在尝试做一些奇怪的事情-为什么您需要重定向到另一条路线?我需要更多详细信息,但是在正常的Symfony2场景中,您可能希望渲染一个Twig模板,并通过以下方式传递这些值:
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
));
这篇关于使用symfony中的参数重定向到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!