Symfony2: How to get user Object inside controller when using FOSUserBundle?

后端 未结 8 1790
别跟我提以往
别跟我提以往 2020-12-28 13:04

I\'m using FOSUserBundle to authenticate my users.

I\'m trying to get the user object inside the Controller to register a trip where I should add the user object to

相关标签:
8条回答
  • 2020-12-28 13:30

    Solution:

    $userManager = $this->container->get('fos_user.user_manager');
    $user = $userManager->findUserByUsername($this->container->get('security.context')
                        ->getToken()
                        ->getUser())
    
    0 讨论(0)
  • 2020-12-28 13:39

    In FOSUser 1.3 you can't call directly $this->getUser in SecurityController.

    You have to call $this->container->get('security.context')->getToken()->getUser();

    And this is enough to access the user object. No need to call $user = $em->getRepository('SiteUtilisateurBundle:Utilisateur')->find($username);

    Furthermore your find method automatically and implicitly cast your initial $username object to string because it doesn't wait an object as argument.

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