TokenStorage sometimes returns null in Service

后端 未结 1 1736
孤独总比滥情好
孤独总比滥情好 2021-01-17 16:47

I have a Service that get\'s the current logged in user, which only works some of the time whilst in the dev environment.

The problem seems to be whenever I change t

1条回答
  •  有刺的猬
    2021-01-17 17:35

    I'm not certain, but it looks to me like your class should maintain a pointer to the tokenStorage class, not the token itself (as this may change). Your service would then look like this:

    class MyService
    {
       private $em;
       private $tokenStorage;
    
       public function __construct($entityManager, TokenStorageInterface $tokenStorage)
       {
           $this->em = $entityManager;
           $this->tokenStorage = $tokenStorage;
       }
    
       public function doSomething()
       {
           $user_id = $this->tokenStorage->getToken()->getUser()->getID();
           return;
       }
    }
    

    0 讨论(0)
提交回复
热议问题