A friend of mine recently had a requirement where she wanted to have two config files loaded into Zend_Application, so that the specific settings for the server were not stored in the version control system.
Hence she has two config files: application.ini and local.ini where local.ini is different on each server.
Update:
As takeshin points out with ZF 1.10 or later, an easier solution to this is to pass both ini files into Zend_Application:
$application = new Zend_Application(
APPLICATION_ENV,
array(
'config' => array(
APPLICATION_PATH . '/configs/application.ini',
APPLICATION_PATH . '/configs/local.ini'
)
)
);
Original content:
The obvious way to approach this problem is to load the two files within index.php, merge them and then pass the merged config file to Zend_Application.
The code to do this looks like this:
require_once 'Zend/Application.php';
require_once 'Zend/Config/Ini.php';
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV,
array('allowModifications'=>true));
$localConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/local.ini', APPLICATION_ENV);
$config->merge($localConfig);
$config->setReadOnly();
// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
$config
);
$application->bootstrap()
->run();
I've included the require_once 'Zend/Application.php'; call for context.
As you can see, we create two Zend_Config objects, one for application.ini and one for local.ini and we load up the correct APPLICATION_ENV section in both cases. This means that local.ini must have the same set of "master" environment sections that application.ini has.
For application.ini, we set the option "allowModifications' so that we can then use the merge() method to override the $config with the new data within $localConfig.
Having merged the local config into the main one, we can then setReadOnly() to ensure that the config isn't changed anymore.
Finally, when we instantiate Zend_Application, we pass in our $config object rather than the name of the config file and then Zend_Application does the right thing.



November 29th, 2010 at 09:03 #
Is it really needed?
Maybe an array will do fine:
$application = new Zend_Application( APPLICATION_ENV, array( 'config' => array( APPLICATION_PATH . '/configs/application.ini', APPLICATION_PATH . '/configs/local.ini' ) ) );November 29th, 2010 at 10:49 #
to: takeshin and to rob to:
Thank you
November 29th, 2010 at 11:20 #
I've typically gone with a template file stored in the vcs with the actual config file ignored. The downside is that it's easy to forget to add your changes back into the template file.
November 29th, 2010 at 18:53 #
I agree with takeshin. Providing an array of config files looks much neater and has been working terribly well for me in production.
November 29th, 2010 at 19:21 #
I recently needed to do this same, but the approach I took was simply convert both the configs into arrays and use array_merge - Didn't realize there was a merge function in the Zend_Config_Object, think I'll change it to use it instead.
November 29th, 2010 at 20:54 #
I really like the idea of having these two configs, but wonder about the performance issues. For every page load you have to load and merge the two configs. Has anyone looked into caching the merged config?
December 7th, 2010 at 00:15 #
I've recently had a similar requirement and I ended up implementing the same solution !
The only difference was as different section for the second file (used to store server options).
Another solution might be extending the Zend_Application class and adding a method to merge two files. However, since the Zend_Application supports options in Zend_Config format, the easiest solution I found was just merging the files in the index.php
December 21st, 2010 at 20:14 #
Is it really needed? Maybe an array will do fine: $application = new Zend_Application( APPLICATION_ENV, array( 'config' => array( APPLICATION_PATH . '/configs/application.ini', APPLICATION_PATH . '/configs/local.ini' ) ) );
December 23rd, 2010 at 21:31 #
I recently needed to do this same, but the approach I took was simply convert both the configs into arrays and use array_merge - Didn't realize there was a merge function in the Zend_Config_Object, think I'll change it to use it instead.