【问题标题】:Symfony 3 Datafixtures using parameter.yml valuesSymfony 3 Datafixtures 使用 parameter.yml 值
【发布时间】:2017-08-23 18:33:29
【问题描述】:

我在我的User 数据装置中使用 LDAP,我不想硬编码 LDAP 登录选项。最初,我尝试了这个:

$options = array(
            'host' => '%ldap_host%',
            'port' => '%ldap_port%',
            'useSsl' => true,
            'username' => '%ldap_username%',
            'password' => '%ldap_password%',
            'baseDn' => '%ldap_baseDn_users%'
        ); 

但这没有用。我做了一些研究,意识到我需要include the container in my fixtures。然而,就在这一点上,我不确定我的下一步是什么。

据我了解,我需要使用容器和 get 方法来获取包含参数的服务,但我不知道那是什么:

$this->container->get('parameters');

不起作用,所以我想知道我应该使用什么。

我的完整数据夹具如下:

class LoadFOSUsers extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function setContainer(ContainerInterface $container = null)
    {
        $this->container = $container;
    }


    public function load(ObjectManager $manager)
    {
        $this->container->get('parameters');

        // Not sure how to access param values. 
        $options = array(
            'host' => '%ldap_host%',
            'port' => '%ldap_port%',
            'useSsl' => true,
            'username' => '%ldap_username%',
            'password' => '%ldap_password%',
            'baseDn' => '%ldap_baseDn_users%'
        );

        $ldap = new Ldap($options);
        $ldap->bind();

        $baseDn = '%ldap_baseDn_users%';
        $filter = '(&(&(ObjectClass=user))(samaccountname=*))';
        $attributes=['samaccountname', 'dn', 'mail','memberof'];
        $result = $ldap->searchEntries($filter, $baseDn, Ldap::SEARCH_SCOPE_SUB, $attributes);

        foreach ($result as $item) {
            echo $item["dn"] . ': ' . $item['samaccountname'][0] . PHP_EOL;
        }
    }

    public function getOrder()
    {
        // the order in which fixtures will be loaded
        // the lower the number, the sooner that this fixture is loaded
        return 8;
    }
}

【问题讨论】:

    标签: php symfony doctrine-orm symfony-3.2


    【解决方案1】:

    您只需通过getParameter('name') 从容器中取出它们,或者通过getParameterBag() 将它们全部放入一个袋子中。

    所以:

        $options = array(
            'host' => $this->container->getParameter('ldap_host'),
            'port' => $this->container->getParameter('ldap_port'),
            'useSsl' => true,
            'username' => $this->container->getParameter('ldap_username'),
            'password' => $this->container->getParameter('ldap_password'),
            'baseDn' => $this->container->getParameter('ldap_baseDn_users')
        );
    

    等等

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多