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;
}
}
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);
}
}
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With