Milos Cuculovic
Milos Cuculovic

Reputation: 20223

Symfony2: User entity class which implements UserInterface: Use email instead of username

I am trying to work with the symfony2 login. I have my entity class connected to the database. In my table, I have only the user's email, which is the username. Should I use the getUsername() method knowing that I don't have a real username.

When I try to delete this method, I am geting a fatal error message :

Fatal error: Class MDPI\BackendBundle\Entity\Users contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Symfony\Component\Security\Core\User\UserInterface::getUsername) in /home/milos/workspace/mdpi-login2/src/MDPI/BackendBundle/Entity/Users.php on line 768 

Should I use

getUsername()
{
  return this->email;
}

Or is there a nicer way of doing this ???

Thank you.

Upvotes: 1

Views: 845

Answers (1)

AlterPHP
AlterPHP

Reputation: 12727

Using the native Symfony SecurityBundle, you just have to specify the entity field you use as login string, in app/config/security.yml :

security:

    encoders:
        MDPI\BackendBundle\Entity\User: plaintext

    providers:
        entity:
            entity: { class: MDPI\BackendBundle\Entity\User, property: email }

    firewalls:
        ...

Upvotes: 2

Related Questions