Snippets - Little bits of code to make you happy
An alternative to _ss_environment Files for easy 'Per Environment' ConfigurationTweet
9 June 2011 | | | Supports v2.4, v2.3
Although Silverstripe has a mechanism for alternative configuration per environment, using the ConfigureFromEnv() method in your mysite/_config.php file, it has its limitations and I have often read people complaining about it. Here is a simple yet powerful alternative to using SilverStripe environment files.
You can read more about the 'Official' method here: http://doc.silverstripe.org/sapphire/en/topics/environment-management.
Instead of using that method, I have found it useful to add the following line to the bottom of /mysite/_config.php file instead:
This attempts to include the _live-settings.php file and fails silently if it is not there. In the development environment we just ignore this file and everything is configured as normal in the _config.php file.
On the live machine we add this new file to mysite/ and in it we can override any of the settings in the standard _config.php file. In particular you can do things like provide live database connection values, override the mailer and email addresses, modify caching and so on. Your _live-config.php might look something like this:
<?php $databaseConfig = array( "type" => "MySQLDatabase", "server" => "localhost", "username" => "username", "password" => "Password", "database" => "database_live", ); Security::setDefaultAdmin('admin','ABetterPassword');
This method gives us a very simple but highly expressive environment specific configuration. What is more, since we don't have any of the live database settings in our _config.php we don't have to worry about storing it in a configuration management system like Subversion or Git since our sensitive data is only stored on the live server.