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
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 :)
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
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.
You can use any option from the following.
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'];
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'];
In module.config.php
return array(
'settings' => array(
'settingA' => 'foo',
'settingB' => 'bar'
)
)
And in controller,
$config = $this->getServiceLocator()->get('Config');
echo $config['settings']['settingA'];