繁体   English   中英

如何从local.php访问配置

[英]How to access config from local.php

我将旧的密码加密类从ZF1迁移到ZF2。 在我的原始代码中,我使用了zend注册表来存储从application.ini文件提取的加密盐值。 在ZF2中,进行此设置的逻辑位置是config / autoload文件夹中的local.php文件。

我的问题是如何访问local.php文件中指定的盐设置?

我努力了

$this->getServiceLocator()->get('Config'); 

但这一切都会产生错误

Call to a member function get() on a non-object
in C:\Users\Garry Childs\Documents\My Webs\freedomw\vendor\freedom\Zend\Filter\EncryptPassword.php on line 59

我也尝试将以下功能添加到我的module.php文件中

public function getConfig()
{
    return include __DIR__ . '/config/module.config.php';
}

并使用

$this->getConfig();

无济于事。

请有人指出正确的方向,谢谢。

会说您必须为密码加密类创建一个工厂,并从工厂中的ServiceLocator获取盐,然后将config中的盐注入类(在构造函数中或使用某些setter)。

所以像这样:

在你的module.config.php

'service_manager' => array(
    'factories' => array(
        'My\Filter\EncryptPassword' => 'My\Filter\EncryptPasswordFactory',
    )
)

然后在您的My\\Filter\\EncryptPasswordFactory.php

<?php
namespace My\Filter;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class EncryptPasswordFactory implements FactoryInterface
{
    /**
     * @param  ServiceLocatorInterface $serviceLocator
     * @return EncryptPasswordService
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config = $serviceLocator->get('config');
        $salt = ...get salt from config...
        $encryptPasswordService = new EncryptPasswordService($salt);
        return $encryptPasswordService;

...或制作setSalt方法或您认为最合适的方法...

    }
}

您的My\\Filter\\EncryptPasswordService.php

<?php
namespace My\Filter;

class EncryptPasswordSevice
{
    /**
     * @param  ServiceLocatorInterface $serviceLocator
     * @return EncryptPasswordService
     */
    public function __construct($salt)
    {
        use your $salt
    }
}

现在,您可以在可以访问My\\Filter\\EncryptPassword实例(或serviceLocator实例)的任何位置获取My\\Filter\\EncryptPassword ,如下所示:

$encryptPasswordService = $serviceManager->get('My\Filter\EncryptPassword');

暂无
暂无

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

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