2009-09-30 4 views
29

어떻게 둘 이상의 매개 변수를 전달하는 다른 동작으로 리디렉션 할 수 있습니까? 이 코드 : URL에심포니 2 매개 변수로 리디렉션

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

결과 :

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

답변

53

음, 정상적인 경우 "리디렉션"하여 절대 URL로 리디렉션합니다. 당신은 할 수 있습니다 :

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

도움이 귀엽다. "http : //.../input/new/year/2009/month/10"과 같은 URL을 생성합니다. – kipelovets

+3

경로가 정의되어 있으면 'default'를 이름으로 대체하고 두 번째 매개 변수를 변경할 수 있습니다 필요한 경우 경로의 매개 변수로 – xarch

+4

경로를 정의했다면 실제로'$ this-> redirectToRoute ('routename', [ 'param1'=> 'value', [ 'param2'=> 'value'])' – chteuchteu

3

나는이 더 정상적인 심포니 동작입니다 생각합니다. 라우팅 규칙을 정의 했습니까?

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

아니, 그게 도움이되지 않습니다 – kipelovets

1

이상한 일이 :

당신이 시도했다. 합니까

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

당신을 위해 일합니까?

+2

네, 그 또한 작동합니다. 고마워,하지만 xarch의 솔루션은 =) – kipelovets

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

을 사용하면 내부적으로 심포니의 라우팅 시스템 – jochil

+1

실제로 그렇지 않습니다. symfony의 지능형 내부 라우팅 엔진이이 리다이렉트가 작동하는 이유입니다! 아래쪽 투표에 감사드립니다.) – Mohammad

+5

이렇게하면 라우팅 규칙을 조정할 때 모든 링크/리디렉션을 변경해야합니다. – jochil

4

또한 경로 이름과 매개 변수 배열 지정, 리디렉션을 사용할 수 있습니다

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

(심포니 1.4에서 테스트)

2

현재 지원에서을 Symfony 버전 (2.7 이상) it's even easier :

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