简体   繁体   中英

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):

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

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 . It was called in UserAuthenticationProvider at line 96 .

If you take a look at that code, you'll see that the 4th argument for UsernamePasswordToken::__construct() is $roles . So that should be an array, but it's getting null instead.

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. 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.

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question zend db error on switching tables Catchable fatal error: Argument 1 passed to __construct() must be an array, object given, called in Catchable fatal error: Argument 1 passed to Illuminate\Config\Repository::__construct() must be of the type array, integer given Catchable Fatal Error: Argument 1 passed to … must be an instance of …, array given PHP Catchable fatal error: Argument 1 passed to must be of the type array, null given, called in on line 208 and defined Modx: Catchable fatal error: Argument 2 passed to modParser::collectElementTags() must be of the type array, null given Catchable Fatal Error: Argument 1 passed to Controller::__construct() must be an instance of Doctrine\ORM\EntityManager, none given, called Symfony2: ContextErrorException: Catchable Fatal Error: Argument 1 passed to […]::__construct() must implement interface […] none given Catchable fatal error: Argument 1 passed to Album\Controller\AlbumController::__construct() must be an instance of Album\Model\AlbumTable, none given Catchable Fatal Error: Argument 1 passed to "…\FormType::__construct() must implement interface 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