38

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

Martin54
  • 1,349
  • 2
  • 13
  • 34
kipelovets
  • 1,204
  • 2
  • 13
  • 16

6 Answers6

58

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)));
xarch
  • 732
  • 5
  • 5
  • 1
    that helped. except for it generates url like "http://.../input/new/year/2009/month/10" – kipelovets Oct 01 '09 at 10:39
  • 3
    If you have a route defined, you can replace 'default' by its name, and change the second parameter with the routes's parameters, if needed. – xarch Oct 01 '09 at 15:37
  • 4
    If you have a route defined, you can actually use `$this->redirectToRoute('routename', ['param1' => 'value', ['param2' => 'value'])` – Quentin S. Jul 13 '15 at 10:50
11

In the currently supported Symfony versions (2.7+) it's even easier (plus, you can optionally add also the status code at the end):

return $this->redirectToRoute(
    'default',
    array('year' => $year, 'month' => $month),
    Response::HTTP_MOVED_PERMANENTLY // = 301
);
forsberg
  • 1,681
  • 1
  • 21
  • 27
5

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)

Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
3

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));
jochil
  • 1,074
  • 6
  • 12
1

Strange thing. Does

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

work for you?

Andrei Dziahel
  • 969
  • 5
  • 14
-3
$this->redirect('input/new/year/' . $year . '/month/' . $month);
Mohammad
  • 714
  • 4
  • 13
  • 3
    this undermine the internal routing system of symfony – jochil Oct 01 '09 at 07:01
  • 1
    it actually doesn't. symfony's intelligent internal routing engine is exactly why this redirect works! but thanks for the down vote ;) – Mohammad Oct 02 '09 at 04:26
  • 6
    when you do it this way you have to change all links/redirects when adjusting the according routing rule – jochil Oct 05 '09 at 12:11