Extends UserManager in Symfony2 with FOSUserBundle

2019-02-07 08:35发布

问题:

I tried to extend the UserManager of FOS according to this link

My service is correctly detected but i have an error i can't resolved :

ErrorException: Catchable Fatal Error: Argument 1 passed to FOS\UserBundle\Entity\UserManager::__construct() must be an instance of Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface, none given, called in 

MyUserManager :

namespace ChoupsCommerce\UserBundle\Model;

use FOS\UserBundle\Entity\UserManager;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;

class MyUserManager extends UserManager
{
    public function loadUserByUsername($username)
    {
        $user = $this->findUserByUsernameOrEmail($username);

        if (!$user) {
            throw new UsernameNotFoundException(sprintf('No user with name "%s" was found.', $username));
        }

        return $user;
    }
}

回答1:

I had the same problem, here is my solution (basically a combination of the first two answers):

Set up service in config.yml, don't forget the arguments

services:
    custom_user_manager:
        class: Acme\UserBundle\Model\CustomUserManager
        arguments: [@security.encoder_factory, @fos_user.util.username_canonicalizer, @fos_user.util.email_canonicalizer, @fos_user.entity_manager, Acme\UserBundle\Entity\User]

Then connect service to FOS user_manager (also in config.yml):

fos_user:
    service:
        user_manager: custom_user_manager

In the CustomUserManager recall the construct-method, like gilden said:

class CustomUserManager extends UserManager{

    public function __construct(EncoderFactoryInterface $encoderFactory, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, ObjectManager $om, $class)
    {
        parent::__construct($encoderFactory, $usernameCanonicalizer, $emailCanonicalizer, $om, $class);
    }

}


回答2:

You need to add the constructor method (__construct) and pass the required arguments down to the base class:

namespace ChoupsCommerce\UserBundle\Model;

use Doctrine\ORM\EntityManager;
use FOS\UserBundle\Entity\UserManager;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use FOS\UserBundle\Util\CanonicalizerInterface;

class MyUserManager extends UserManager
{
    public function __construct(
        EncoderFactoryInterface $encoderFactory,
        CanonicalizerInterface $usernameCanonicalizer,
        CanonicalizerInterface $emailCanonicalizer,
        EntityManager $em,
        $class
    ) {
        parent::__construct($encoderFactory, $usernameCanonicalizer, $emailCanonicalizer, $em, $class);
    }
}


回答3:

Sorry my English is poor.

May be your custom manager service's arguments is wrong because it says

Argument 1 passed to FOS\UserBundle\Entity\UserManager::__construct() must be an instance of Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface, none given, called in

Here is my config:

iep_core.iep_user_manager:   
     class: Iep\CoreBundle\Service\IepUserManager
     arguments: [@security.encoder_factory, @fos_user.util.username_canonicalizer, @fos_user.util.email_canonicalizer, @fos_user.entity_manager, Iep\CoreBundle\Entity\User]