Symfony how to return all logged in Active Users

前端 未结 5 2044
-上瘾入骨i
-上瘾入骨i 2020-12-05 01:31

I want to return all Logged in users of my application and render it in my Dashboard. The user_id and user_name should be retrieve

5条回答
  •  心在旅途
    2020-12-05 01:46

    In Symfony 4 I solved the problem in the following way.

    em = $em;
            $this->security = $security;
        }
    
        public function onTerminate() {
            $user = $this->security->getUser();
    
            if (!$user->isActiveNow()) {
                $user->setLastActivityAt(new \DateTime());
                $this->em->persist($user);
                $this->em->flush($user);
            }
        }
    
        public static function getSubscribedEvents() {
            return [
                // must be registered before (i.e. with a higher priority than) the default Locale listener
                KernelEvents::TERMINATE => [['onTerminate', 20]],
            ];
        }
    
    }
    

提交回复
热议问题