2012-04-02 287 views

回答

0

您可以將重定向路徑作爲GET參數(例如redirectTo)傳遞到編輯頁面,編輯過程完成後,重定向到該路徑。

return new RedirectResponse($request->query->get('redirectTo'); 

你可以把通過檢查是否已提供的參數更強勁,如果不是,重定向到某種默認路徑。

+0

嗨,我想避免與GET參數搞亂:) 會話不能用於這樣的事情嗎? – bodokaiser 2012-04-02 18:44:15

+0

會話會變得非常混亂 - 在那裏,做到了。 – 2012-04-02 18:49:56

+0

其他解決方案? – bodokaiser 2012-04-03 05:45:42

1

您的控制器內部爲/profile/edit,您可以使用$request->headers->get('referer')捕獲它們來自的頁面。

如果/profile/edit是一個帶有單一窗體的頁面,我可能只是添加一個隱藏字段,說明重定向應該放在哪裏。

public function editAction(Request $request) 
{ 
    // If you have a POST value coming from the user, it will be used, otherwise 
    // assume this is the first time they landed on the page and grab the current 
    // referer. With this method it doesn't matter how many times they submit the form 
    // you won't accidentally overwrite the referer URL with /profile/edit. That could 
    // lead to a confusing loop. 
    $referer = $request->request->get('referer', $request->headers->get('referer')); 

    if ($formIsSaved) 
    { 
     return new RedirectResponse($referer) 
    } 

    return array(
     // Your template should include a hidden field in the form that returns this. 
     'referer' => $referer, 
    ); 
} 
相關問題