Symfony登录不起作用

时间:2017-09-25 09:52:42

标签: php symfony symfony-2.8

我使用symfony 2.8,我是symfony的新手,我已经实现了登录和注册,注册工作正常,但是当我登录时显示此错误

Type error: Argument 4 passed to Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct() must be of the type array, string given, 
called in C:\xampp\htdocs\blog\vendor\symfony\symfony\src\Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider.php on line 96

现在我在ROLES实现中有点困惑,我在DB中有用户表,

USERS TABLE

id      Primary     int(11)
name                varchar(255)    
email   Index       varchar(255) 
password            varchar(64)
roles               varchar(255) 
created_at          datetime 

用户实体

public function setRoles($roles) {
    $this->roles = $roles;
}

public function getRoles() {
        return $this->roles;
}

Security.yml 防火墙部分

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        anonymous: ~
        form_login:
            login_path: login
            check_path: login
        pattern:    ^/
        http_basic: ~
        provider: our_db_provider
        # activate different ways to authenticate

        # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
        #http_basic: ~

        # https://symfony.com/doc/current/security/form_login_setup.html
        #form_login: ~
        logout:
            path: /logout
            target: /

如果我改变我的getRoles函数以返回这样的数组

public function getRoles() {
    return array('ROLE_USER');
}

在这种情况下,它在注册页面上显示错误。

The value of type "array" cannot be converted to a valid array key.

1 个答案:

答案 0 :(得分:0)

看起来你正在为你的角色传递一个字符串。将它作为一个数组传递!

public function __construct($user, $credentials, $providerKey, array $roles = array())

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Core/Authentication/Token/UsernamePasswordToken.php#L34

因此,如果角色为Admin,请尝试传递array('admin')