简体   繁体   English

可捕获的致命错误:传递给UsernamePasswordToken :: __ construct()的参数4必须是一个数组,给定null

[英]Catchable Fatal Error: Argument 4 passed to UsernamePasswordToken::__construct() must be an array, null given

I am getting the following error when logging in into my Symfony application (with correct username and password): 登录到我的Symfony应用程序时(使用正确的用户名和密码),我收到以下错误:

ContextErrorException: Catchable Fatal Error: Argument 4 passed to Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken::__construct() must be an array, null given, called in D:\\xampp\\htdocs\\essweb\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\Security\\Core\\Authentication\\Provider\\UserAuthenticationProvider.php on line 96 and defined in D:\\xampp\\htdocs\\essweb\\vendor\\symfony\\symfony\\src\\Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken.php line 36 ContextErrorException:可捕获的致命错误:传递给Symfony \\ Component \\ Security \\ Core \\ Authentication \\ Token \\ UsernamePasswordToken :: __ construct()的参数4必须是一个数组,给定null,在D:\\ xampp \\ htdocs \\ essweb \\ vendor \\ symfony中调用第96行的\\ symfony \\ src \\ Symfony \\ Component \\ Security \\ Core \\ Authentication \\ Provider \\ UserAuthenticationProvider.php,在D:\\ xampp \\ htdocs \\ essweb \\ vendor \\ symfony \\ symfony \\ src \\ Symfony \\ Component \\ Security \\ Core中定义\\ Authentication \\ Token \\ UsernamePasswordToken.php第36行

Security.yml Security.yml

firewalls:
        admin_area:
            pattern:    ^/
            anonymous: ~
            form_login:
                login_path:  /
                check_path:  /login_check
                default_target_path: /user
                failure_path: /
                #username_parameter: username
                #password_parameter: password
                remember_me: true

            remember_me:
                key: "%secret%"
                lifetime: 31536000
                path: /
                domain: ~
                always_remember_me: true
            logout:
                path:   /logout
                target: /  

Updated: 更新:

Login form: 登录表格:

<form class="form-signin form-group" action="{{ path('login_check') }}" method="post">
Username: <input type="text" class="form-control" name="_username" placeholder="Username" value="{{ last_username }}" required="required">
Password: <input type="text" class="form-control" name="_password" placeholder="" value="{{ last_username }}" required="required">
<button class="btn btn-sm btn-primary btn-block" type="submit">Sign in</button>

Basically, what the error message says is: 基本上,错误消息的内容是:

The 4th argument for UsernamePasswordToken::__construct() should be an array, but it's null . UsernamePasswordToken::__construct()的第四个参数应该是一个数组,但它是null It was called in UserAuthenticationProvider at line 96 . 在第96行的UserAuthenticationProvider调用。

If you take a look at that code, you'll see that the 4th argument for UsernamePasswordToken::__construct() is $roles . 如果您查看该代码,您将看到UsernamePasswordToken::__construct()的第4个参数是$roles So that should be an array, but it's getting null instead. 所以这应该是一个数组,但它变为null

I'm guessing that you have written your own User entity , and that the getRoles() method of your user entity is returning null instead of an array of roles. 我猜你已经编写了自己的User实体 ,并且用户实体getRoles()方法返回null而不是一组角色。 So just change that method to something like this: 所以只需将该方法更改为以下内容:

/**
 * Returns the roles granted to the user.
 * 
 * @return Role[] The user roles
 */
public function getRoles()
{
    return array('ROLE_USER');
}

Of course, the actual code may differ (you might want to store the user roles in the database), as long as getRoles() returns an array of strings or Role objects. 当然,只要getRoles()返回字符串数组或Role对象,实际代码可能会有所不同(您可能希望将用户角色存储在数据库中)。

I fix this error so returning roles: 我修复此错误,以便返回角色:

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

}

This error is catching because the roles property current provided value is null however you have to give it an array of roles, so to resolve it just return the roles propoerty : 此错误正在捕获,因为角色属性当前提供的值为null,但是您必须为其提供一组角色,因此要解决它只返回角色propoerty:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 切换表时出现zend db错误可捕获的致命错误:传递给__construct()的参数1必须是一个数组,给定对象,在 - zend db error on switching tables Catchable fatal error: Argument 1 passed to __construct() must be an array, object given, called in 可捕获的致命错误:传递给 Illuminate\\Config\\Repository::__construct() 的参数 1 必须是数组类型,给定整数 - Catchable fatal error: Argument 1 passed to Illuminate\Config\Repository::__construct() must be of the type array, integer given 可捕获的致命错误:传递给...的参数1必须是...,给定数组的实例 - Catchable Fatal Error: Argument 1 passed to … must be an instance of …, array given PHP可捕获的致命错误:传递给参数1的类型必须为数组类型,给定为null,在第208行调用并定义 - PHP Catchable fatal error: Argument 1 passed to must be of the type array, null given, called in on line 208 and defined Modx:可捕获的致命错误:传递给modParser :: collectElementTags()的参数2必须为数组类型,给定为null - Modx: Catchable fatal error: Argument 2 passed to modParser::collectElementTags() must be of the type array, null given 可捕获的致命错误:传递给Controller :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityManager的实例,未给出任何实例,称为 - Catchable Fatal Error: Argument 1 passed to Controller::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called Symfony2:ContextErrorException:可捕获的致命错误:传递给[…] :: __ construct()的参数1必须实现接口[…]没有给出 - Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to […]::__construct() must implement interface […] none given 可捕获的致命错误:传递给 Album\Controller\AlbumController::__construct() 的参数 1 必须是 Album\Model\AlbumTable 的实例,没有给出 - Catchable fatal error: Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Model\AlbumTable, none given 可捕获的致命错误:传递给“…\\ FormType :: __ construct()的参数1必须实现接口 - Catchable Fatal Error: Argument 1 passed to "…\FormType::__construct() must implement interface 可捕获的致命错误:传递给AppBundle \\ Form \\ TagType :: __ construct()的参数1必须是Doctrine \\ ORM \\ EntityRepository的实例,未给出任何实例, - Catchable Fatal Error: Argument 1 passed to AppBundle\Form\TagType::__construct() must be an instance of Doctrine\ORM\EntityRepository, none given,
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM