Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将Symfony存储库注入自定义用户提供程序?_Php_Symfony_Dependency Injection - Fatal编程技术网

Php 如何将Symfony存储库注入自定义用户提供程序?

Php 如何将Symfony存储库注入自定义用户提供程序?,php,symfony,dependency-injection,Php,Symfony,Dependency Injection,我以前已经成功地将存储库注入到服务中,但无法让注入在自定义用户提供程序中工作。似乎永远不会调用_construct()方法,因此存储库永远不会可用。创建用户提供程序的方法是实现UserProviderInterface接口,但创建的UserProvider类不会扩展另一个具有构造函数的类。当我尝试使用$this->personRepository->findStatusByUsername($username)访问存储库时,会出现以下错误: 不在对象上下文中使用$this 我认为构造函数永远不会

我以前已经成功地将存储库注入到服务中,但无法让注入在自定义用户提供程序中工作。似乎永远不会调用_construct()方法,因此存储库永远不会可用。创建用户提供程序的方法是实现UserProviderInterface接口,但创建的UserProvider类不会扩展另一个具有构造函数的类。当我尝试使用$this->personRepository->findStatusByUsername($username)访问存储库时,会出现以下错误:

不在对象上下文中使用$this

我认为构造函数永远不会被调用的一个原因是它有两个$this赋值,但我没有收到关于它们的错误消息

我需要注入Person存储库,以便检查尝试进行身份验证的人员是否已在应用程序中获得批准(Person表中的状态=已批准)

My services.yml文件具有以下设置:

parameters:
  ginsberg_transportation.user.class: Ginsberg\TransportationBundle\Services\User
  user_provider.class: Ginsberg\TransportationBundle\Security\User\UserProvider

services:
  ginsberg_user:
    class: "%ginsberg_transportation.user.class%"
  user_provider:
    class: "%user_provider.class%"
    arguments:
      ["@ginsberg_person.person_repository", "@logger"]
  ginsberg_person.person_repository:
    class: Doctrine\ORM\EntityRepository
    factory_service: doctrine.orm.default_entity_manager
    factory_method: getRepository
    arguments:
      - Ginsberg\TransportationBundle\Entity\Person
My UserProvider.php类的开头如下:

namespace Ginsberg\TransportationBundle\Security\User;

use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Ginsberg\TransportationBundle\Entity\Person;
use Doctrine\ORM\EntityRepository;

class UserProvider implements UserProviderInterface
{
  private $personRepository;
  private $logger;

  public function __construct(\Ginsberg\TransportationBundle\Entity\PersonRepository $personRepository, \Monolog\Logger $logger) {
    $this->personRepository = $personRepository;
    $this->logger = $logger;
  }

    public function loadUserByUsername($uniqname)
    {
      $password = "admin";
      $salt = "";
      $roles = array();

      if (self::is_authenticated() && self::is_approved()) {
        if (self::is_superuser()) {
          $roles[] = 'ROLE_SUPER_ADMIN';
        } elseif (self::is_admin()) {
          $roles[] = 'ROLE_ADMIN';
        } elseif (self::is_eligible() && self::is_approved()) {
          $roles[] = 'ROLE_USER';
        }

        return new User($uniqname, $password, $salt, $roles);
      }

      throw new UsernameNotFoundException(
      sprintf('Username "%s" does not exist.', $uniqname));
    }
...
    public static function is_approved()
    {
      $uniqname = self::get_uniqname();
      $status = $this->personRepository->findStatusByUsername($username);

      return($status == 'approved') ? TRUE : FALSE;      
    }
我也尝试过使用属性注入,但也不起作用

我应该让我的UserProvider类扩展另一个适当的类来使用构造函数吗?如果是这样的话,你认为什么课程最合适


谢谢您的建议。

问题在于方法
是经过批准的()
静态的
。您不能在静态上下文中使用
$this
。您需要声明您的方法是非静态的。

您是否在安全性中集成了您的服务和自定义用户提供程序。yml?factory_服务和factory_方法已被弃用。因此,您应该使用
factory:[“@doctrine.orm.entity\u manager”,“getRepository”]
。我不敢相信我做了那件事。谢谢