目标:当用户登录时,在整个网站上根据状态重定向用户。
我需要强制用户在一个页面上,直到他更改了他的个人资料所以我尝试使用事件内核进行重定向,但我有一个无限循环。但是,一旦页面需要,我尝试避免执行此重定向
所以请找到我尝试做的事情
class TokenSubscriber implements EventSubscriberInterface {
private $user;
private $tokenStorage;
private $router;
protected $redirect;
public function __construct(TokenStorageInterface $tokenStorage, RouterInterface $router
) {
$this->tokenStorage = $tokenStorage;
$this->router = $router;
}
public function onKernelController(FilterControllerEvent $event) {
$controller = $event->getController();
if (!is_array($controller)) {
return;
}
$this->user = $this->tokenStorage->getToken()->getUser();
if ($this->user->getValider() == 3 && $controller[1] == 'indexUserAction' && $controller[0] instanceof DefaultUserController) {
$this->redirect = null;
} else {
$this->redirect = 'user_index';
}
}
public function onKernelResponse(FilterResponseEvent $event) {
if (null !== $this->redirect) {
$url = $this->router->generate($this->redirect);
$event->setResponse(new RedirectResponse($url));
}
}
public static function getSubscribedEvents() {
return array(
KernelEvents::CONTROLLER => 'onKernelController',
KernelEvents::RESPONSE => 'onKernelResponse',
);
}
}