3
我正在創建電子商務應用,並且正在使用Handler構建自定義重定向。Symfony2/FosUserBundle - 在登錄前獲取上一條路由
比方說,我的用戶在他的購物車上,他沒有登錄。他驗證他的購物車繼續購買過程。在這種情況下,我的應用將用戶重定向到登錄頁面。但是當他最終登錄時,我想將他重定向到之前的路線(即購物車頁面)。 (對不起,我的英文不好,我希望你能理解我說的)
這是我AuthentificationSuccessHandler:
class AuthentificationSuccessHandler implements AuthenticationSuccessHandlerInterface
{
protected $router;
protected $security;
/**
* AfterLoginRedirection constructor.
* @param Router $router
* @param AuthorizationChecker $security
*/
public function __construct(Router $router, AuthorizationChecker $security)
{
$this->router = $router;
$this->security = $security;
}
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$session = $request->getSession();
if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
$response = new RedirectResponse($this->router->generate('_homepage_admin'));
} else {
$referer_url = $request->headers->get('referer');
$response = new RedirectResponse($referer_url);
}
$session->getFlashBag()->add('success', 'Connexion réussi !');
return $response;
}
正如你所看到的,我用headers->get('referer');
喜歡我的文檔中看到,但這不起作用。用戶重定向到登錄頁面(但認證工作正常)
所以我卡住了現在..
告訴我,如果你需要更多的代碼,以幫助我爲我的問題。
任何線索或建議?
謝謝。
這篇文章也許可以幫[http://stackoverflow.com/questions/32649157/reliably-redirect-to -last-accessible-page-in-symfony2] –
謝謝。那麼我想我會使用會話來存儲路線。 – Letsrocks