Redirect Symfony2:重定向到请求的路径

Redirect Symfony2:重定向到请求的路径,redirect,symfony,controller,Redirect,Symfony,Controller,我想将用户重定向回他启动请求的路径 例如: /侧面图 /配置文件/编辑 /侧面图 或: /产品 /配置文件/编辑 /产品 此重定向模式需要设置什么?您可以将重定向路径作为GET参数传递到编辑页面,例如,重定向到,编辑过程完成后,重定向到该路径 返回新的重定向响应($request->query->get('redirectTo'); 您可以通过检查是否提供了该参数使其更加健壮,如果未提供,则重定向到某种默认路径。在控制器中,对于/profile/edit,您可以使用$request->head

我想将用户重定向回他启动请求的路径

例如: /侧面图

/配置文件/编辑

/侧面图

或:

/产品

/配置文件/编辑

/产品


此重定向模式需要设置什么?

您可以将重定向路径作为GET参数传递到编辑页面,例如,
重定向到
,编辑过程完成后,重定向到该路径

返回新的重定向响应($request->query->get('redirectTo');

您可以通过检查是否提供了该参数使其更加健壮,如果未提供,则重定向到某种默认路径。

在控制器中,对于
/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,
    );
}

嗨,我想避免弄乱GET参数:)会话不能用于这样的事情吗?会话将变得非常混乱-在那里,完成了。