UserProvider没有实现正确的接口

时间:2015-03-28 09:39:23

标签: php symfony fosuserbundle fosoauthserverbundle

我正在尝试将自定义UserProvider与FOS用户捆绑包和FOS Oauth包一起使用。我一直在关注本教程http://blog.tankist.de/blog/2013/07/17/oauth2-explained-part-2-setting-up-oauth2-with-symfony2-using-fosoauthserverbundle/

当我尝试注册我的新用户服务时,我收到以下错误

UserProvider::__construct() must implement interface Doctrine\Common\Persistence\ObjectRepository, array given

UserProvider:

UserProvider implements UserProviderInterface
{
/**
 * @var ObjectRepository
 */
protected $userRepository;

/**
 * @param ObjectRepository $userRepository
 */
public function __construct(ObjectRepository $userRepository)
{
    $this->userRepository = $userRepository;
}

/**
 * @param string $username
 * @return mixed
 */
public function loadUserByUsername($username)
{ ... }

UserRepository

class UserRepository extends EntityRepository implements ObjectRepository

services.yml

parameters:
  entity_name:  Hornby\UserBundle\Entity\UserRepository

services:
  user_provider_me:
  class: Hornby\UserBundle\Provider\UserProvider
  arguments:
    name: [%entity_name%]

1 个答案:

答案 0 :(得分:2)

您的services.yml文件错误。我不确定你想要实现什么,但你传递数组

arguments:
    name: [%entity_name%] #entity_name is just a string

作为UserProvider类的参数。这个类的构造函数需要ObjectRepository,这是你的问题。

您提供的链接中的services.xml翻译应该类似于:

parameters:
    platform.entity.user.class: Acme\DemoBundle\Entity\User
    platform.user.provider.class: Acme\DemoBundle\Provider\UserProvider
services:
    platform.user.manager:
        class: Doctrine\ORM\EntityManager
        factory: ["@doctrine", getManagerForClass]
        arguments: [%platform.entity.user.class%]

    platform.user.repository:
        class: Acme\DemoBundle\Repository\UserRepository
        factory: ["@platform.user.manager", getRepository]
        arguments: [%platform.entity.user.class%]

    platform.user.provider:
        class: %platform.user.provider.class%
        arguments: [@platform.user.repository]