簡體   English   中英

Symfony2:重定向到請求的路徑

[英]Symfony2: Redirect to requested path

我想將用戶重定向回他發起請求的路徑。

示例:/ profile

/個人資料/編輯

/輪廓

要么:

/產品

/個人資料/編輯

/產品

我必須為該重定向模式設置什么?

/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參數(例如redirectTo )傳遞到編輯頁面,然后在完成編輯過程后將其重定向到該路徑。

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

您可以通過檢查是否提供了該參數來使其更健壯,如果沒有提供,則重定向到某種默認路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM