Symfony2 OAuth2 提供程序错误 UserRepository.



我是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 "AcmeDemoBundleRepository" in C:xampphtdocs*****vendordoctrineormlibDoctrineORMRepositoryDefaultRepositoryFactory.php line 75. Do you need to "use" it from another namespace?

我认为代码可能会导致此错误:

<!-- src/Acme/DemoBundle/Resources/config/services.xml -->
<parameters>
    <parameter key="platform.entity.user.class">AcmeDemoBundleEntityUser</parameter>
    <parameter key="platform.user.provider.class">AcmeDemoBundleProviderUserProvider</parameter>
</parameters>
<services>
    <service id="platform.user.manager" class="DoctrineORMEntityManager"
             factory-service="doctrine" factory-method="getManagerForClass">
        <argument>%platform.entity.user.class%</argument>
    </service>
    <service id="platform.user.repository"
             class="AcmeDemoBundleRepositoryUserRepository"
             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>

<?php
  // src/Acme/DemoBundle/Provider/UserProvider.php
  namespace AcmeDemoBundleProvider;
  use SymfonyComponentSecurityCoreUserUserInterface;
  use SymfonyComponentSecurityCoreUserUserProviderInterface;
  use SymfonyComponentSecurityCoreExceptionUsernameNotFoundException;
  use SymfonyComponentSecurityCoreExceptionUnsupportedUserException;
  use DoctrineCommonPersistenceObjectRepository;
  use DoctrineORMNoResultException;
  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());
      }
  }

但我无法理解...

我遇到了同样的问题,这就是我所做的。

实体文件夹中创建名为 UserRepository.PHP 的文件

在里面,把这个代码

命名空间登录\

登录捆绑包\实体;

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

使用正确的命名空间、捆绑包名称、类名称等。这对我有用,希望它也对你有用。

正确答案如下:

在以下位置创建一个新文件夹存储库:src/Acme/DemoBundle/

并使用以下代码创建新文件 UserRepository.php。

<?php 
namespace AcmeDemoBundleRepository;
use DoctrineORMEntityRepository;
/**
* UserRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class UserRepository extends EntityRepository
{
}

这是纠正错误的方法,如果您注意到错误,它指出在"Acme\DemoBundle\Repository"中找不到类UserRepository。所以很简单,只需在该目录中创建该类即可。

我在Symfony2书中找到了一个简单的修复程序

在项目根目录的命令行中尝试此操作:

$ php app/console doctrine:generate:entities YourAppBundle

这将有助于将实体按正确的顺序排列,并可能解决问题。它对我有用。 : )

相关内容

  • 没有找到相关文章

最新更新