Symfony 登录无效 [英] Symfony Login Not working
本文介绍了Symfony 登录无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 symfony 2.8,我是 symfony 的新手,我已经实现了登录和注册,注册工作正常但是当我登录时显示此错误
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
现在我对 ROLES 实现有点困惑,我在数据库中有用户表,
Now I'm bit confused in ROLES implementation, I have users table in DB,
用户表
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 函数来返回这样的数组
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.
推荐答案
看起来您正在为您的角色传递一个字符串.将其作为数组传递!
Looks like you are passing a string for your role. Pass it as an array!
public function __construct($user, $credentials, $providerKey, array $roles = array())
因此,如果角色是 Admin
,请尝试传递 array('admin')
.
So if the role was Admin
, try passing array('admin')
.
这篇关于Symfony 登录无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文