Chuck Norris Chuck Norris - 1 year ago 227
PHP Question

symfony 3 Doctrine LIstener service inject token_storage doesnt work

I have a doctrine listener which needs the get the current logged in user.

class DoctrineListener
* @var null|TokenInterface
private $token;

* DoctrineListener constructor.
* @param TokenStorageInterface $tokenStorage
public function __construct(TokenStorageInterface $tokenStorage)
$this->token = $tokenStorage->getToken();


and in my service.yml:

class: AppBundle\EventListener\DoctrineListener
- '@security.token_storage'
public: false
- { name: doctrine.event_listener, event: preFlush, method: preFlush }

The dump always returns me null when I try to use it in this listener.
I inject the token_storage_service in other services and it works well.

I'm under symfony 3.1, with a rest API.
And i send my authorizations header with Postman.

Can someone tell me what's wrong with my code ?

Thanks in advance.

Answer Source

Try to call $tokenStorage->getToken() in you preFlush method not in the constructor.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download