Quantcast
Channel: Active questions tagged symfony4 - Stack Overflow
Viewing all articles
Browse latest Browse all 3924

Symfony - Get security.token_storage in Sylius repositories

$
0
0

I have tried to inject security.token_storage service via CompilerPass, I do not have errors and user is currently logged, however when I access the TokenStorage, $tokenStorage->getToken() I got a null response.

I checked these links to reference, but I cannot achieve to get a valid token.

How to inject shared service from compiler pass in Symfony and Access currently logged in user in UserRepository in Sylius

Do you an idea why it returns null response?

  • Sylius version: 1.4 and Symfony 4.4

Bundle class:

   public function build(ContainerBuilder $container)
   {
       parent::build($container);
       $container->addCompilerPass(new ModifyRepositoryPass());
   }

ModifyRepositoryPass class:

class ModifyRepositoryPass implements CompilerPassInterface
{

   public function process(ContainerBuilder $container)
   {
       $container
           ->getDefinition('sylius.repository.order')
           ->addMethodCall('getTokenStorage', array(
               new Reference('security.token_storage')
           ));

    }
  }

Get token:

class OrderRepository extends BaseOrderRepository
{

    /** @var TokenStorageInterface */
    protected $tokenStorage;

    public function getTokenStorage(TokenStorageInterface $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }

    public function createListFilter()
    {

        $token = $this->tokenStorage->getToken()); //  NULL token
    }



Viewing all articles
Browse latest Browse all 3924

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>