重定向路由上的无限循环

时间:2018-05-17 15:29:47

标签: php symfony

我有一个问题,我不知道如何阻止它。我在重定向路由上有一个无限循环,如果用户不接受某些条件,我会尝试重定向用户 这是我的代码:

 /**
     * AcceptanceListener constructor.
     * @param $router
     * @param TokenStorageInterface $tokenStorage
     * @param UserManager $userManager
     * @param AuthorizationChecker $checker
     */
    public function __construct(
        $router,
        TokenStorageInterface $tokenStorage,
        UserManager $userManager,
        AuthorizationChecker $checker
    ) {
        $this->router = $router;
        $this->tokenStorage = $tokenStorage;
        $this->userManager = $userManager;
        $this->role = $checker;
    }

    /**
     * To checked all the routes
     *
     * @param GetResponseEvent $event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        $user = $this->tokenStorage->getToken()->getUser();
        if ($this->role->isGranted('ROLE_USER')
            && !$this->userManager->hasAccepted($user)
        ) {
            $this->redirect = $this->router->generate('accept_');
            $event->setResponse(new RedirectResponse($this->redirect));
        }
    }
有人可以帮我吗?

1 个答案:

答案 0 :(得分:2)

检查路线是否不相同,然后重定向

public function onKernelRequest(GetResponseEvent $event)
    {
        $route_name = $event->getRequest()->get('_route');

            if ($route_name != 'accept_') {

        $user = $this->tokenStorage->getToken()->getUser();
        if ($this->role->isGranted('ROLE_USER')
            && !$this->userManager->hasAccepted($user)
        ) {
            $this->redirect = $this->router->generate('accept_');
            $event->setResponse(new RedirectResponse($this->redirect));
        }
    }
}