Symfony2: How to get user Object inside controller

2019-02-02 06:11发布

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 this Trip before save.

I did not found how to do that because next method where I found it in symfony doc:

$user = $this->container->get('security.context')->getToken()->getUser();

renders the username as string, but I need the whole object.

Currently, I use this method, but it's not working properly.

$username = $this->container->get('security.context')->getToken()->getUser();
$em = $this->container->get('doctrine')->getEntityManager();
$user = $em->getRepository('SiteUtilisateurBundle:Utilisateur')->find($username);

How can I correctly do this?

8条回答
别忘想泡老子
2楼-- · 2019-02-02 06:51

The documentation for the getUser method indicates:

either returns an object which implements __toString(), or a primitive string is returned.

And if we look in the FOS\UserBundle\Model\User class over here (the base user class used by the FOSUserBundle) we can see that it does indeed have a __toString method:

public function __toString()
{
    return (string) $this->getUsername();
}

I think that you actually get the User object but because it implements a __toString method it can be rendered directly in templates.

In Twig you can use:

{{ dump(user) }}

To see what kind of object you have. But You are actually using an object, not a string.

查看更多
做自己的国王
3楼-- · 2019-02-02 06:58

Solution:

$userManager = $this->container->get('fos_user.user_manager');
$user = $userManager->findUserByUsername($this->container->get('security.context')
                    ->getToken()
                    ->getUser())
查看更多
做个烂人
4楼-- · 2019-02-02 07:02

I think Ramon is right. You already have the user object.

Also in Symfony > 2.1.x you can use

$this->getUser();

inside the controller.

查看更多
【Aperson】
5楼-- · 2019-02-02 07:03
public function indexAction()
{
    /* @var $user \FOS\UserBundle\Model\UserInterface */
    if ($user = $this->getUser())
    {
        echo '<pre>';
        print_r($user);
        print_r($user->getRoles()); // method usage example

        exit;

        return $this->redirectToRoute('dashboard');
    }

    return $this->redirectToRoute('login');
}
查看更多
倾城 Initia
6楼-- · 2019-02-02 07:08

I had the same issue, to resolve it add the FOS classes in your use section i.e:

use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Model\UserInterface;
查看更多
女痞
7楼-- · 2019-02-02 07:14

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.

查看更多
登录 后发表回答