UserProvider 没有实现正确的接口

UserProvider not implementing correct interface

我正在尝试将自定义 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 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)
{ ... }

用户存储库

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%]

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

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

作为 UserProvider class 的参数。此 class 的构造函数期望 ObjectRepository,这是您的问题。

您提供的 link 的 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]