简体   繁体   English

ZF2全局配置设置

[英]ZF2 global config settings

My goal is to have configuration settings shared and editable within each module in a Zend Framework 2 project. 我的目标是在Zend Framework 2项目的每个模块中共享和编辑配置设置。 After research, I came to this: http://akrabat.com/zend-framework-2/injecting-configuration-into-a-zf2-controller/ which worked great at first but after my application keeps expanding to more modules and more controllers, I don't appreciate this solution. 经过研究,我来到了这里: http : //akrabat.com/zend-framework-2/injecting-configuration-into-a-zf2-controller/起初效果很好,但是在我的应用程序不断扩展到更多模块之后,控制器,我不喜欢这种解决方案。 Precisely, I don't appreciate having to "implement" interface, add SetConfig method and so on. 确切地说,我不喜欢必须“实现”接口,添加SetConfig方法等等。 Instead, I'd like to be able to just call on $GLOBALS or similar in my controller and get the parameter of interest. 相反,我希望能够仅在控制器中调用$ GLOBALS或类似名称并获取感兴趣的参数。

Say I have a file config.local.php with the following format: 说我有一个具有以下格式的文件config.local.php:

    return array(
    'group1' => array(
        'key1' => 'value1',
        'key2' => 'value2',
    )

   'group2' => array (
        'subgroup' => array (
             'keyA' => 'valueA',
             'keyB' => 'valueB',
        ),
    )
);

Right now, i'm following the tutorial above and after implemeting the ConfigAwareInterface in my controller, I get valueA using 现在,我正在遵循上面的教程,并在我的控制器中实现了ConfigAwareInterface之后,使用

$this->config['group2']['subgroup']['keyA']

I'm now hoping to just rather call $GLOBALS['group2']['subgroup']['keyA'] without the ConfigAwareInterface intermediate. 我现在希望仅在没有ConfigAwareInterface中间对象的情况下调用$ GLOBALS ['group2'] ['subgroup'] ['keyA']。

I laid the following quick pseudo code to help me achieve that: 我放置了以下快速伪代码来帮助实现这一目标:

for every this->config as key => value 
    if $key is not array
        $GLOBALS[$key] => $value
    else 
        for every $value as $k => $v
            $GLOBALS[$key][$k] => $value

As ZF2 and php are new to me, I'm not completely sure I can have more than 1 index/nested keys for $GLOBALS but I can tailor my config file to avoid that. 由于ZF2和php是我的新手,因此我不确定我是否可以为$ GLOBALS使用多个索引/嵌套键,但是我可以定制配置文件来避免这种情况。

There are probably other logical mistakes above. 上面可能还有其他逻辑错误。 But just to get the point. 但是只是为了指出要点。 Now, is this the best way to go about global configuration or is there a better approach? 现在,这是进行全局配置的最佳方法还是有更好的方法?

Thanks in advance. 提前致谢。

You can always easily access the configuration via: 您始终可以通过以下方式轻松访问配置:

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

What Akrabat did was to provide a shortHand getConfig() - Akrabat所做的是提供shortHand getConfig() -

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

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