Search code examples
zend-framework2

Where to put custom settings in Zend Framework 2?


I have some custom application specific settings, I want to put in a configuration file. Where would I put these? I considered /config/autoload/global.php and/or local.php. But I'm not sure which key(s) I should use in the config array to be sure not to override any system settings.

I was thinking of something like this (e.g. in global.php):

return array(
    'settings' => array(
        'settingA' => 'foo',
        'settingB' => 'bar',
    ),
);

Is that an agreeable way? If so, how can I access the settings e.g. from within a controller?

Tips are highly appreciated.


Solution

  • In case you need to create custom config file for specific module, you can create additional config file in module/CustomModule/config folder, something like this:

    module.config.php
    module.customconfig.php
    

    This is content of your module.customconfig.php file:

    return array(
        'settings' => array(
            'settingA' => 'foo',
            'settingB' => 'bar',
        ),
    );
    

    Then you need to change getConfig() method in CustomModule/module.php file:

    public function getConfig() {
        $config = array();
        $configFiles = array(
            include __DIR__ . '/config/module.config.php',
            include __DIR__ . '/config/module.customconfig.php',
        );
        foreach ($configFiles as $file) {
            $config = \Zend\Stdlib\ArrayUtils::merge($config, $file);
        }
        return $config;
    }
    

    Then you can use custom settings in controller:

     $config = $this->getServiceLocator()->get('config');
     $settings = $config["settings"];
    

    it is work for me and hope it help you.