简体   繁体   中英

Get the entity that represents the current user [symfony2]

How do I get the email address of the current user ?

To retrieved the Username or user id is easy , but i didn't found any example that explain how to get the email address of the current use.

$entity = new Post();
 $userManager = $this->container->get('fos_user.user_manager');
            $usr = $userManager->findUserByUsername($this->container->get('security.context')
                ->getToken()
                ->getUser());

            $entity->setUsername($usr);

In controller (that extends symfony controller)

$this->getUser();

For services you need to inject security.token_storage service and use

$this->tokenStorage()->getToken()->getUser()

TokenStorage::getToken() can return null, consider it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM