Separating config from code in Zend Framework is not as simple as it is in say, Ruby on Rails. And when your source code is version-controlled you want to minimise the manual configuration required. So here’s a nice way to keep it simple. The idea is to keep configuration data for your test and production environments in a single config file that does not change, then switch environments in a dedicated separate one-line config file.
Here’s a sample platforms.ini:
[test] developer_mode = true db.adapter = PDO_MYSQL db.params.host = localhost db.params.dbname = testdb db.params.username = lkdf034k3k3b db.params.password = SJGS43we3sdd [production] developer_mode = false db.adapter = PDO_MYSQL db.params.host = localhost db.params.dbname = productiondb db.params.username = ksjdhfkjjdhf db.params.password = jfdhkjsdhdjk
.. .And here’s active_platform.ini. This is where we define which of test or production is currently active:
[platform] setting = test
…And here’s how index.php refers to active_platform.ini to first determine which platform it’s on, and second load the appropriate config from platforms.ini:
// which platform mode are we on? i.e.: test or production $active_platform = new Zend_Config_Ini('application/configs/active_platform.ini','active_platform'); // get the platform config $config = new Zend_Config_Ini('application/configs/platforms.ini', $active_platform->setting); Zend_Registry::set('config', $config); // set up the db $db = Zend_Db::factory($platform_config->db); Zend_Db_Table::setDefaultAdapter($db);
When you want to deploy to production, you change the setting in active_platform.ini to ‘production.’ Simple as that.. this may be obvious but I’ve found it to speed up deployment and make it less error-prone.