简体   繁体   English

如何从local.php访问配置

[英]How to access config from local.php

I am moving my old password encrypt class from ZF1 to ZF2. 我将旧的密码加密类从ZF1迁移到ZF2。 In my original code I used the zend registry to store the encryption salt value pulled from the application.ini file. 在我的原始代码中,我使用了zend注册表来存储从application.ini文件提取的加密盐值。 In ZF2 the logical place for this setting to go would be the local.php file in config/autoload folder. 在ZF2中,进行此设置的逻辑位置是config / autoload文件夹中的local.php文件。

My question is how do I access the salt setting specified in the local.php file? 我的问题是如何访问local.php文件中指定的盐设置?

I have tried 我努力了

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

but all this does is produce the error 但这一切都会产生错误

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

I have also tried to add the following function to my module.php file 我也尝试将以下功能添加到我的module.php文件中

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

and used 并使用

$this->getConfig();

to no avail. 无济于事。

Please can someone point me in the right direction, many thanks. 请有人指出正确的方向,谢谢。

would say that you have to make a factory for your password encrypt class and get the salt from ServiceLocator in the factory and inject the salt from the config into your class (either in constructor or by using some setter). 会说您必须为密码加密类创建一个工厂,并从工厂中的ServiceLocator获取盐,然后将config中的盐注入类(在构造函数中或使用某些setter)。

So something like this: 所以像这样:

In your module.config.php : 在你的module.config.php

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

And then in your My\\Filter\\EncryptPasswordFactory.php : 然后在您的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;

...or make a setSalt method or whatever you think is nicest... ...或制作setSalt方法或您认为最合适的方法...

    }
}

Your My\\Filter\\EncryptPasswordService.php : 您的My\\Filter\\EncryptPasswordService.php

<?php
namespace My\Filter;

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

Now you can get your My\\Filter\\EncryptPassword anywhere where you have access to a serviceManager instance (or serviceLocator instance) like this: 现在,您可以在可以访问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