Symfony2:实现UserInterface的用户实体类:使用电子邮件而不是用户名

时间:2012-09-24 14:04:57

标签: authentication logging symfony

我正在尝试使用symfony2登录。我将我的实体类连接到数据库。在我的表中,我只有用户的电子邮件,即用户名。我应该使用getUsername()方法知道我没有真正的用户名。

当我尝试删除此方法时,我发出致命的错误消息:

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 

我应该使用

getUsername()
{
  return this->email;
}

或者有更好的方法吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

使用本机Symfony SecurityBundle,您只需在app/config/security.yml中指定用作登录字符串的实体字段:

security:

    encoders:
        MDPI\BackendBundle\Entity\User: plaintext

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

    firewalls:
        ...
相关问题