问题
I'm using symfony 2.8, I'm new to symfony,I have implemented login and registration, registration is working fine but when I login it showing this error
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
Now I'm bit confused in ROLES implementation, I have users table in DB,
USERS TABLE
id Primary int(11)
name varchar(255)
email Index varchar(255)
password varchar(64)
roles varchar(255)
created_at datetime
User Entity
public function setRoles($roles) {
$this->roles = $roles;
}
public function getRoles() {
return $this->roles;
}
Security.yml firewalls section
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: /
If I change my getRoles function to return array like this
public function getRoles() {
return array('ROLE_USER');
}
In this case it is showing error on registration page.
The value of type "array" cannot be converted to a valid array key.
回答1:
Looks like you are passing a string for your role. Pass it as an array!
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
So if the role was Admin, try passing array('admin').
来源:https://stackoverflow.com/questions/46402312/symfony-login-not-working