All,
I have a config xml file in the following format:
<?xml version="1.0"?>
<configdata>
<development>
<siteTitle>You are doing Development</siteTitle>
</development>
<test extends="development">
<开发者_如何学编程;siteTitle>You are doing Testing</siteTitle>
</test>
<production extends="development">
<siteTitle>You are in Production</siteTitle>
</production>
</configdata>
To read this config file to apply environment settings, currently I am using, the following code in index.php file:
$appEnvironment = "production";
$config = new Zend_Config_Xml('/config/settings.xml', $appEnvironment );
To deploy this code on multiple environments, as user has to change index.php file. Instead of doing that, is it possible to maintain an attribute in the xml file, "say active=true". Based on which the Zend_Config_Xml will know which section of the xml file settings to read?
If you're using Zend Framework, I highly suggest you just use the APPLICATION_ENV constant that's defined in Zend Application's default index.php. If you're not, you could probably do this regardless.
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
It corresponds to your the environmental variable set in your site's .htaccess
file:
//.htaccess
SetEnv APPLICATION_ENV development
You could then load your config file like so:
$config = new Zend_Config_Xml('/config/settings.xml', APPLICATION_ENV );
Then to switch environments, all that needs to be done is switch the value in the .htaccess file. On my server, I have my deployment script set up to automatically switch the value based on which environment I'm deploying to:
$ sed -i 's/development/production/' /path/to/site/root/.htaccess
You could, instead, have a unique bootstrap file for development and a unique bootstrap file for production.
- index.php.production
- index.php.development
These two files can be under source control. A deployment script would copy the appropriate bootstrap to index.php and away you go. The index.php file can, and should, be ignored by source control - otherwise you are just stepping on toes.
Of course, the specialized bootstrap files would contain the appropriate environment string.
There are a couple of ways to handle this, probably the easiest would be setting the application environment constant in your vhost file.
If you don't have access to the vhost file (shared hosting, for example), you can place a separate config file in a location on the server that's available in all environments.
You might place server.xml in /home/username (pick a directory that's available in all of your environments).
server.xml might look like
<?xml version="1.0"?>
<configdata>
<properties>
<environment>production</environment>
</properties>
</configdata>
Load server.xml as you would any other config file
$server = new Zend_Config_Xml('/home/username/server.xml', 'properties');
Finally, you can load your config file across multiple environments without having to edit anything in your application.
$config = new Zend_Config_Xml('/config/settings.xml', $server->environment );
The one gotcha here is you must ensure your server.xml files are in place before deploying your app.
精彩评论