简体   繁体   English

Symfony2 OAuth2提供程序错误UserRepository

[英]Symfony2 OAuth2 provider error UserRepository

I'm new in Symfony framework and using tutorial http://blog.tankist.de/blog/2013/07/17/oauth2-explained-part-2-setting-up-oauth2-with-symfony2-using-fosoauthserverbundle/ getting error: 我是Symfony框架的新手,并使用教程http://blog.tankist.de/blog/2013/07/17/oauth2-explained-part-2-setting-up-oauth2-with-symfony2-using-fosoauthserverbundle/得到错误:

ClassNotFoundException: Attempted to load class "UserRepository" from namespace "Acme\\DemoBundle\\Repository" in C:\\xampp\\htdocs\\*****\\vendor\\doctrine\\orm\\lib\\Doctrine\\ORM\\Repository\\DefaultRepositoryFactory.php line 75. Do you need to "use" it from another namespace?

I think that code can cause this error: 我认为该代码可能会导致此错误:

<!-- src/Acme/DemoBundle/Resources/config/services.xml -->
<parameters>
    <parameter key="platform.entity.user.class">Acme\DemoBundle\Entity\User</parameter>
    <parameter key="platform.user.provider.class">Acme\DemoBundle\Provider\UserProvider</parameter>
</parameters>

<services>
    <service id="platform.user.manager" class="Doctrine\ORM\EntityManager"
             factory-service="doctrine" factory-method="getManagerForClass">
        <argument>%platform.entity.user.class%</argument>
    </service>

    <service id="platform.user.repository"
             class="Acme\DemoBundle\Repository\UserRepository"
             factory-service="platform.user.manager" factory-method="getRepository">
        <argument>%platform.entity.user.class%</argument>
    </service>

    <service id="platform.user.provider" class="%platform.user.provider.class%">
        <argument type="service" id="platform.user.repository" />
    </service>
</services>

OR 要么

<?php
  // src/Acme/DemoBundle/Provider/UserProvider.php

  namespace Acme\DemoBundle\Provider;

  use Symfony\Component\Security\Core\User\UserInterface;
  use Symfony\Component\Security\Core\User\UserProviderInterface;
  use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  use Doctrine\Common\Persistence\ObjectRepository;
  use Doctrine\ORM\NoResultException;

  class UserProvider implements UserProviderInterface
  {
      protected $userRepository;

      public function __construct(ObjectRepository $userRepository){
          $this->userRepository = $userRepository;
      }

      public function loadUserByUsername($username)
      {
          $q = $this->userRepository
              ->createQueryBuilder('u')
              ->where('u.username = :username OR u.email = :email')
              ->setParameter('username', $username)
              ->setParameter('email', $username)
              ->getQuery();

          try {
              $user = $q->getSingleResult();
          } catch (NoResultException $e) {
              $message = sprintf(
                  'Unable to find an active admin AcmeDemoBundle:User object identified by "%s".',
                  $username
              );
              throw new UsernameNotFoundException($message, 0, $e);
          }

          return $user;
      }

      public function refreshUser(UserInterface $user)
      {
          $class = get_class($user);
          if (!$this->supportsClass($class)) {
              throw new UnsupportedUserException(
                  sprintf(
                      'Instances of "%s" are not supported.',
                      $class
                  )
              );
          }

          return $this->userRepository->find($user->getId());
      }

      public function supportsClass($class)
      {
          return $this->userRepository->getClassName() === $class
          || is_subclass_of($class, $this->userRepository->getClassName());
      }
  }

But I can not understand... 但我无法理解......

I faced the same problem, here is what I did. 我遇到了同样的问题,这就是我所做的。

Create a file named UserRepository.PHP in Entity folder Entity文件夹中创建名为UserRepository.PHP的文件

Inside it, put this code 在里面,把这个代码

namespace Login\\LoginBundle\\Entity; 命名空间Login \\ LoginBundle \\ Entity;

use Doctrine\ORM\EntityRepository;

/**
 * UserRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class UserRepository extends EntityRepository
{
}

Use your proper namespace, bundle name, class name etc. This worked for me, hope it will work for you too. 使用适当的命名空间,包名称,类名等。这对我有用,希望它也适合你。

The correct answer is below: 正确的答案如下:

Create a new folder Repository under: src/Acme/DemoBundle/ 在以下位置创建一个新文件夹Repository:src / Acme / DemoBundle /

And create new file UserRepository.php with the below code. 并使用以下代码创建新文件UserRepository.php。

<?php 
namespace Acme\DemoBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
* UserRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class UserRepository extends EntityRepository
{
}

This is what will correct the error, if you notice the error it states that class UserRepository was not found in "Acme\\DemoBundle\\Repository". 这是纠正错误的原因,如果您发现错误,则表明在“Acme \\ DemoBundle \\ Repository”中找不到类UserRepository。 So its simple just create that class in that directory. 所以它的简单只是在该目录中创建该类。

I found a simple fix in the Symfony2 Book 我在Symfony2 Book中找到了一个简单的修复程序

Try this in the command line at the root of your project: 在项目根目录的命令行中尝试:

$ php app/console doctrine:generate:entities YourAppBundle

It will help put the Entities in proper order and likely fix the problem. 它将有助于使实体处于正确的顺序并可能解决问题。 It worked for me. 它对我有用。 : ) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM