2012-04-02 236 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, 
    ); 
} 
相关问题