Home » Php » php – symfony redirect with 2 parameters

php – symfony redirect with 2 parameters

Posted by: admin April 23, 2020 Leave a comment

Questions:

how can i redirect to another action passing 2 or more parameters? this code:

$this->redirect('input/new?year=' . $year . '&month=' . $month);

results in URL:

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

How to&Answers:

Well, that’s normal, “redirect” redirect to an absolute URL. You can do that:

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

Answer:

In currently supported Symfony versions (2.7+) it’s even easier:

return $this->redirectToRoute('default', array('year' => $year, 'month' => $month));

Answer:

You can also use redirect, specifying the route name and the parameter array:

$this->redirect('route_name', array('year' => $year, 'month' => $month));

(Tested on Symfony 1.4)

Answer:

I think this is no normal symfony behavior. Have you defined some routing rules?

Have you also tried this:

$this->redirect('module/action?'.http_build_query($paramsArray));

Answer:

Strange thing. Does

$this->redirect('@default?module=input&action=new&year=' . $year . '&month=' . $month);

work for you?

Answer:

$this->redirect('input/new/year/' . $year . '/month/' . $month);