简体   繁体   中英

ZF2 global config settings

My goal is to have configuration settings shared and editable within each module in a Zend Framework 2 project. 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. Precisely, I don't appreciate having to "implement" interface, add SetConfig method and so on. Instead, I'd like to be able to just call on $GLOBALS or similar in my controller and get the parameter of interest.

Say I have a file config.local.php with the following format:

    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

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

I'm now hoping to just rather call $GLOBALS['group2']['subgroup']['keyA'] without the ConfigAwareInterface intermediate.

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.

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() -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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