SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Tutorials - Big bits of code to help you do more

Using SilverStripe Environment files for Database Configuration

Wouldn't it be easy to have a single environment configuration for all your SilverStripe sites without having to reconfigure each one individually? Wouldn't it be great not to have to change environment settings when you push your site from development to live servers? This is easy to achieve using the SilverStripe "ConfigureFromEnv" script.

To set this up, remove your $databaseConfig line from your site _config.php and add:

mysite/_config.php

require_once("conf/ConfigureFromEnv.php");

Then create a new _ss_environment.php file. This can live in your web root, in it's parent or it's parent parent folder. Define the following constants:

_ss_enviroment.php

/* What kind of environment is this: development, test, or live (ie, production)? */
define('SS_ENVIRONMENT_TYPE', 'dev/test/live');
 
/* Database connection */
define('SS_DATABASE_SERVER', 'localhost');
define('SS_DATABASE_USERNAME', 'root');
define('SS_DATABASE_PASSWORD', '');
 
/* Configure a default username and password to access the CMS on all sites in this environment. */
define('SS_DEFAULT_ADMIN_USERNAME', 'username');
define('SS_DEFAULT_ADMIN_PASSWORD', 'password');

Now your _config.php file only needs one setting, a $database parameter that points SilverStripe to the correct database for this server!

Hamish Campbell avatar

Hamish Campbell

Hamish is a web developer for Opus International Consultants in New Zealand. He creates tools for managing data online, with a particular interest in geographical information systems. He is also an active contributor to the SilverStripe open source project.

  • Adrian van Vliet
    23/12/2010 11:29am (4 years ago)

    Can someone remove the comment above (and this one?) This doesnt work stable enough. Caused some serious problems upon trying this on my live environment. It's a true shame though - what's the point of using _ss_enviroment.php for different database-login-details, when you the database names as just as unique as the login details you log in with. Now I still have to ignore _config in my SVN Repository. This is exactly what I was hoping to fix.

  • jcwacky
    25/02/2012 2:33pm (2 years ago)

    Is there a way to specify a different database name yet?

    This works will for my dev and live sites, as they're on different servers. But my staging site is on the same server as my live site, so it has a different database name.

Post a comment ...

You cannot post comments until you have logged in. Login Here.

Advertisement

Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image Daniel Hensby
3 article image Marcus Dalgren
4 article image Hamish Campbell
5 article image njorndare
6 article image Ty Barho
7 article image Martijn van Nieuwenhoven
8 article image Darren-Lee
9 article image Roman Schmid
10 article image Matt Clegg

View full leaderboard


Advertisement