5

When using a multi-environment config, Craft will select which settings to use based on a partial string match of the server name.

Given the following example, does "dev.mywebsite.com" need to be first or last? Clearly it contains "mywebsite.com" within it, so the order is relevant.

return array(
    // All environments
    '*' => array(
        'tablePrefix' => 'craft',
    ),
    // Production
    'mywebsite.com' => array(
        'server'   => 'localhost',
        'user'     => 'xxxxxxxx',
        'password' => 'xxxxxxxx',
        'database' => 'prod_mywebsite',
    ),
    // Development
    'dev.mywebsite.com' => array(
        'server'   => 'localhost',
        'user'     => 'xxxxxxxx',
        'password' => 'xxxxxxxx',
        'database' => 'dev_mywebsite',
    ),
);
12

In my experience, the order does matter. The array is read in order from top to bottom testing the domain for a match against each criteria (key). If there's a match, that array of settings is used and continues down the list which can possibly overwrite variables that were previously set.

For example, in this configuration...

// Development
'dev.mywebsite.com' => array(
    'database' => 'dev_mywebsite',
),
// Production
'mywebsite.com' => array(
    'database' => 'prod_mywebsite',
),

Your development site http://dev.mywebsite.com will be matched twice, once with the full url dev.mywebsite.com and second as a partial match for mywebsite.com. The second match will overwrite the database setting and your dev site won't load properly.

The Craft documentation example uses the domain criteria of '.com' and '.dev' with '.com' coming after '.dev'. That works fine because example.com will never partially match example.dev.

The "Foolproof Multi-Environment Config" from Craft Cookbook works by matching specific server name local.domainname.com or stage.domainname.com first and using a the production environment as a fallback.

Similarly, you could use the config order to your advantage and only change settings as they're needed. The '*' array can be used as a default/fallback environment.

For example:

// Default environment: Production
'*' => array(
    'tablePrefix' => 'craft',
    'server'   => 'localhost',
    'user'     => 'xxxxxxxx',
    'password' => 'xxxxxxxx',
    'database' => 'prod_mywebsite',
),
// Development
'dev.' => array(
    'user'     => 'xxxxxxxx',
    'password' => 'xxxxxxxx',
    'database' => 'dev_mywebsite',
),

Notice this setup doesn't change the server setting since the dev environment also uses localhost.

Also, I've seen setups where the fallback settings are the Development or Local instead of Production. The advantage there is that your Production site data would be protected from errors in your config file. I've actually made a mistake where the staging server domain didn't match the config file and changes from the staging server were happening to the Production database (bad!) because it was the fallback.

Here's an example from Focus Lab's EE Master Config for ExpressionEngine: https://github.com/focuslabllc/ee-master-config/blob/master/config/config.env.php

As a side note, the "Foolproof Multi-Environment Config" could probably use the strtolower() function on the 'HTTP_HOST' variable as Bjørn Børresen points out in a commit message in the EE Master Config.

switch (strtolower($_SERVER['HTTP_HOST']))

HTTP_HOST should be made lowercase as one cannot guarantee that it always is (most browsers do this, but not all) - in which case the wrong config will be loaded

| improve this answer | |
  • Wow, excellent analysis Alex! Very interesting to know about the cascading nature of these settings... this is exactly the kind of information I was looking for. Thanks! – Lindsey D Nov 8 '14 at 21:26
  • As the original author of the Foolproof Multi-Environment Config, I've gone ahead and added the strtolower. Thanks for the tip! – Lindsey D Nov 8 '14 at 21:41
3

Yes, I've also found that in this case "dev.mywebsite.com" has to be after "mywebsite.com".

It's a bit counter intuitive. Lately I've been using the "Foolproof Multi-Environment Config" from Craft Cookbook, mostly because it keeps the config files more sane.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.