Getting the user id from logged in user in FOSUserBundle
You don't need to create new user object $user = new User();
,you can the current logged in user object from security context
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$user->getId();
Edit: security.context
is deprecated, use security.token_storage
instead. For more details : https://symfony.com/blog/new-in-symfony-2-6-security-component-improvements
simply writing
$user = $this->getUser()->getId();
You get the Id of the current user
This is working for me with Symfony 3
if( $this->container->get( 'security.authorization_checker' )->isGranted( 'IS_AUTHENTICATED_FULLY' ) )
{
$user = $this->container->get('security.token_storage')->getToken()->getUser();
$username = $user->getUsername();
}