Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zend Framework separating developer-specific environment info from config?

I am just getting started with a big Zend Framework project. One thing confuses me so far: much of the configuration seems to be stored in a single file at application/configs/application.ini. This file includes various things essential to the application's function such as app namespace, controller plugins, etc. and also the database login details. I have some external contractors that I need to get working on this project, but I don't want them to have all my database details. In Zend how do you separate the environment info such as db logins (which may be different for different developers) from the application config (which will be the same for everyone)?

like image 816
Chrisj Avatar asked Jul 27 '11 23:07

Chrisj


2 Answers

I agree that this is more difficult in ZF than it should be, hopefully it's an area that will be improved in future versions.

In the meantime, it is quite easy to merge two config files with Zend_Config, and this is something you can use to your advantage. If you open public/index.php you'll see a section near the bottom where it creates an instance of Zend_Application. By default the second parameter is the full path to your config file, but you can pass in an existing Zend_Config object instead. So you create two config objects: application.ini and environment.ini (call this whatever you like), merge them together, and then pass this to Zend_Application:

$config = new Zend_Config_Ini(
    APPLICATION_PATH.'/configs/application.ini', 
    APPLICATION_ENV, 
    array('allowModifications' => true)
);
$environment = new Zend_Config_Ini(
    APPLICATION_PATH.'/configs/environment.ini', 
    APPLICATION_ENV
);

$config->merge($environment);

$application = new Zend_Application(APPLICATION_ENV, $config);
$application->bootstrap()
            ->run();

With this approach, you keep all the standard stuff in application.ini and move your database stuff to environment.ini. You then keep application.ini in source control, add environment.ini to gitignore/svn:ignore, and create a dummy environment.ini.dist which your other developers can use to setup their local projects.

Note: If you have commented out the require_once calls in ZF's library files for performance reasons, you may need to require in some of the Zend_Config classes in public/index.php to get this to work. This should be obvious from the errors though.

like image 83
Tim Fountain Avatar answered Oct 14 '22 19:10

Tim Fountain


What i do for Symfony and Zend is not check in config files with info like that.

Instead a i make config.dist file which i keep in SVN/Git which has all the necessary info to run the app, and then dummy info for sensitive credentials and what not. Then on each machine (local dev, production, staging) i copy this file to the real cnfig file name and edit it with the appropriate environment specific details.

like image 33
prodigitalson Avatar answered Oct 14 '22 18:10

prodigitalson