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

后端 未结 8 1871
别跟我提以往
别跟我提以往 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

    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.

提交回复
热议问题