使用FOSUserBundle扩展Symfony2中的UserManager

时间:2012-03-21 14:14:34

标签: symfony extends fosuserbundle

我尝试根据此link

扩展FOS的UserManager

我的服务被正确检测到,但我有一个错误,我无法解决:

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;
    }
}

3 个答案:

答案 0 :(得分:18)

我有同样的问题,这是我的解决方案(基本上是前两个答案的组合):

在config.yml中设置服务,不要忘记参数

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]

然后将服务连接到FOS user_manager(也在config.yml中):

fos_user:
    service:
        user_manager: custom_user_manager

在CustomUserManager中调用构造方法,就像gilden所说:

class CustomUserManager extends UserManager{

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

}

答案 1 :(得分:4)

您需要添加构造函数方法(__construct)并将所需的参数传递给基类:

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);
    }
}

答案 2 :(得分:2)

抱歉,我的英语很差。

可能是您的自定义管理器服务的参数错误,因为它说

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

这是我的配置:

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]