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

前端 未结 3 531
小蘑菇
小蘑菇 2021-01-11 18:42

I am getting the following error when logging in into my Symfony application (with correct username and password):

ContextErrorException: Catchable Fa

相关标签:
3条回答
  • 2021-01-11 19:33

    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;
    }
    
    0 讨论(0)
  • 2021-01-11 19:34

    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.

    0 讨论(0)
  • 2021-01-11 19:43

    I fix this error so returning roles:

    public function getRoles()
    {
        return $this->roles->toArray();
    
    }
    
    0 讨论(0)
提交回复
热议问题