简体   繁体   English

Symfony2/FosUserBundle - 登录前获取之前的路由

[英]Symfony2/FosUserBundle - Get the previous route before Login

I'm creating an E-commerce app and I'm building a custom redirection with the Handler.我正在创建一个电子商务应用程序,并且正在使用 Handler 构建自定义重定向。

Let's say my user is on his shopping's cart and he is not logged.假设我的用户在他的购物车中并且他没有登录。 He validates his cart to continue the buying process.他验证他的购物车以继续购买过程。 In this situation, My app redirect the user to the login page.在这种情况下,我的应用程序将用户重定向到登录页面。 But when He is finally logged I would like to redirect him to the previous route (ie the cart page).但是当他最终登录时,我想将他重定向到之前的路线(即购物车页面)。 (Sorry for my poor english, I hope you will understand what I say) (对不起,我的英语不好,我希望你能理解我说的话)

This is my AuthentificationSuccessHandler :这是我的 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;
}

As you can see, I use headers->get('referer');如您所见,我使用headers->get('referer'); like I saw in the documentation, but this is not working.就像我在文档中看到的那样,但这不起作用。 The user is redirect to the login page (but the authentification works fine)用户被重定向到登录页面(但身份验证工作正常)

So i'm stuck right now..所以我现在卡住了..

Tell me if you need more code to help me for my issue.如果您需要更多代码来帮助我解决我的问题,请告诉我。

Any clues or advices ?任何线索或建议?

Thanks.谢谢。

Well, I found a solution, I don't know if it's the better way but at least it's works.好吧,我找到了一个解决方案,我不知道它是否是更好的方法,但至少它有效。

So : into my listener I storage a attribute in the session's object, This attribue is update if the user is on a path in my list.所以:在我的侦听器中,我在会话对象中存储了一个属性,如果用户在我的列表中的路径上,则此属性会更新。

My Listener :我的听众:

class RedirectionListener
{

/**
 * RedirectionListener constructor.
 * @param ContainerInterface $container
 * @param Session $session
 */
public function __construct(ContainerInterface $container, Session $session)
{
    $this->session = $session ;
    $this->router = $container->get('router') ;
    $this->securityContext = $container->get('security.context') ;
}

/**
 * @param GetResponseEvent $event
 */
public function onKernelRequest(GetResponseEvent $event){
    $request        = $event->getRequest() ;
    $list_route     = array('_cart', '_category', '_delivery','_validate','_product','_homepage');
    $route          = $request->attributes->get('_route') ;
    $route_params   = $request->attributes->get('_route_params') ;

    if(in_array($route, $list_route)){
        $this->setRouteSession($request, $route, $route_params);
    }
    if($route == "_delivery" || $route =="_validate"){
        if ($this->session->has('cart')){
            if(count($this->session->get('cart')) == 0){
                $this->session->getFlashBag()->add('info', 'Votre panier étant vide, vous ne pouvez pas continuer le processus d\'achat ');
                $event->setResponse(new RedirectResponse($this->router->generate('_cart')));
            }
        }
        if(!is_object($this->securityContext->getToken()->getUser())){
            $this->session->getFlashBag()->add('info', 'Vous devez vous identifier');
            $event->setResponse(new RedirectResponse($this->router->generate('fos_user_security_login')));
        }
    }
}

/**
 * @param $request
 * @param $route
 * @param null $param
 */
private function setRouteSession($request, $route, $param){
    $session = $request->getSession() ;
    $session->set('lastPath', array(
        'route' => $route,
        'params' => $param
    ));
  }
}

My Handler :我的处理程序:

public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
    $session = $request->getSession();
    if(!$session->has('lastPath')){
        $route = '_homepage';
    } else {
        $route = $session->get('lastPath') ;
    }

    if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
        $response = new RedirectResponse($this->router->generate('_homepage_admin'));
    } else {
        if($route['params'] != null){
            $response = new RedirectResponse($this->router->generate($route['route'],$route['params']));
        } else {
            $response = new RedirectResponse($this->router->generate($route['route']));
        }

    }
    $session->getFlashBag()->add('success', 'Connexion réussi !');
    return $response;
}

用户正确登录后,您可以使用

$this->getRequest()->headers->get('referer')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM