How do I get the entity that represents the current user in Symfony2?

前端 未结 6 592
囚心锁ツ
囚心锁ツ 2020-12-04 09:05

I am using the Symfony security setup. Everything works fine, but I don\'t know how to do one important thing:

In twig, I can reach the current user\'s info by doing

6条回答
  •  再見小時候
    2020-12-04 09:55

    The thread is a bit old but i think this could probably save someone's time ...

    I ran into the same problem as the original question, that the type is showed as Symfony\Component\Security\Core\User\User

    It eventually turned out that i was logged in using an in memory user

    my security.yml looks something like this

    security:
        providers:
            chain_provider:
                chain:
                    providers: [in_memory, fos_userbundle]
            fos_userbundle:
                id: fos_user.user_manager
            in_memory:
                memory:
                    users:
                        user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                        admin: { password: adminpass, roles: [ 'ROLE_ADMIN', 'ROLE_SONATA_ADMIN' ] }
    

    the in_memory user type is always Symfony\Component\Security\Core\User\User if you want to use your own entity, log in using that provider's user.

    Thanks, hj

提交回复
热议问题