symfony重定向有2个参数

kip*_*ets 31 php redirect symfony1

如何重定向到另一个传递2个或更多参数的动作?这段代码:

$this->redirect('input/new?year=' . $year . '&month=' . $month);
Run Code Online (Sandbox Code Playgroud)

结果在URL中:

http://.../input?year=2009&month=9

xar*_*rch 55

嗯,这是正常的,"重定向"重定向到绝对URL.你可以这样做:

$this->redirect($this->generateUrl('default', array('module' => 'input',
'action' => 'new', 'year' => $year, 'month' => $month)));
Run Code Online (Sandbox Code Playgroud)

  • 如果你定义了一个路径,你实际上可以使用`$ this-> redirectToRoute('routename',['param1'=>'value',['param2'=>'value'])` (4认同)
  • 如果定义了路径,则可以使用其名称替换"default",并根据需要使用routes的参数更改第二个参数. (3认同)

for*_*erg 7

在目前支持的Symfony版本(2.7+)中,它更容易:

return $this->redirectToRoute('default', array('year' => $year, 'month' => $month));
Run Code Online (Sandbox Code Playgroud)


Gui*_*rez 5

您还可以使用重定向,指定路由名称和参数数组:

$this->redirect('route_name', array('year' => $year, 'month' => $month));
Run Code Online (Sandbox Code Playgroud)

(在 Symfony 1.4 上测试)