Where to put custom settings in Zend Framework 2?

北城以北 提交于 2019-12-03 10:12:26

问题


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.


回答1:


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.




回答2:


You use your module.config.php

return array(
    'foo' => array(
        'bar' => 'baz'
    )

  //all default ZF Stuff
);

Inside your *Controller.php you'd call your settings via

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

It's as simple as that :)




回答3:


You can use any option from the following.

Option 1

Create one file called config/autoload/custom.global.php. In custom.global.php

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

And in controller,

$config = $this->getServiceLocator()->get('Config');
echo $config['settings']['settingA'];

Option 2

In config\autoload\global.php or config\autoload\local.php

return array(
    // Predefined settings if any
    'customsetting' => array(
        'settings' => array(
            'settingA' => 'foo',
            'settingB' => 'bar'
         )
    )
)

And in controller,

$config = $this->getServiceLocator()->get('Config');
echo $config['customsetting']['settings']['settingA'];

Option 3

In module.config.php

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

And in controller,

$config = $this->getServiceLocator()->get('Config');
echo $config['settings']['settingA'];



回答4:


If you look in config/application.config.php it says:

'config_glob_paths'    => array(
    'config/autoload/{,*.}{global,local}.php',
),

So ZF2 by default will autoload configuration files from config/autoload/ - so for example you could have myapplication.global.php it would get picked up and added into the configuration.

Evan.pro wrote a blog post that touches on this: https://web.archive.org/web/20140531023328/http://blog.evan.pro/environment-specific-configuration-in-zend-framework-2



来源:https://stackoverflow.com/questions/12993136/where-to-put-custom-settings-in-zend-framework-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!