Cus­tom Mul­ti-Envi­ron­ment Con­fig and Exam­ples of How to Cus­tomize Each Environment

By default, when you setup a multi-environment config, Craft compares the key in your multi-environment config array to the PHP $_SERVER['SERVER_NAME'] variable to test which environment matches. If the key in your array is contained within the $_SERVER['SERVER_NAME'] variable, Craft considers the option a match.

If you want to be more explicit, you can define each of your environments using the CRAFT_ENVIRONMENT variable in your index.php file and assign each environment a short, keyword for each of your environments.

<?php ## public/index.php

// Check the SERVER_NAME variable ourselves
switch ($_SERVER['SERVER_NAME']) 
{	
    // If the SERVER_NAME variable matches our case, 
    // assign the CRAFT_ENVIRONMENT variable a keyword 
    // that identifies this environment that we can 
    // use in our multi-environment config
    
    case 'straightupcraft.com' :
        define('CRAFT_ENVIRONMENT', 'live');
        break;

    case 'dev.straightupcraft.com' :
        define('CRAFT_ENVIRONMENT', 'dev');
        break;

    default :
        define('CRAFT_ENVIRONMENT', 'local');
        break;
}

?>

You can then use the keywords you've defined in your multi-environment config setup in your craft/config/general.php and craft/config/db.php files.

<?php ## craft/config/general.php

return array(
  '*' => array(
    // Config overrides for all of our environments
  ),

  'live' => array(
    // Config overrides for our live environment
  ),

  'dev' => array(
    // Config overrides for our dev environment
  ),

  'local' => array(
    // Config overrides for our live environment
  ),
);

?>

Craft has sensible defaults that work well, so how you customize your general.php files is based on your personal preference. You can override default config settings or create custom variables that fit your needs. If you choose to customize your settings, below are a handful of examples of some settings that may be useful to consider changing in different environments.

<?php ## craft/config/general.php

/**
 * General Configuration
 *
 * This example multi-environment config file is a 
 * collection of several possible use cases for your
 * multi-environment workflow
 */

// Ensure our urls have the right scheme
define('URI_SCHEME', ( isset($_SERVER['HTTPS'] ) ) ? "https://" : "http://" );

// The site url
define('SITE_URL', URI_SCHEME . $_SERVER['SERVER_NAME'] . '/');

// The site basepath
define('BASEPATH', realpath(CRAFT_BASE_PATH . '/../') . '/');


return array(

	// Config settings for ALL environments
	// ------------------------------------------------------------
	'*' => array(

		// We can use these variables in the URL and Path settings within
		// the Craft Control Panel.  i.e. siteUrl => {siteUrl}, basePath => {basePath} 
		'environmentVariables' => array(
			'siteUrl'  => SITE_URL,
			'basePath' => BASEPATH
		),

		// We can append this value to our CSS and JS files
		// so we can cachebust them all if we need to.
		// <link rel="stylesheet" href="/css/style.css?v={{ craft.config.cacheBustValue }}" /> 
		'cacheBustValue' => '20121017',

		// Create a custom variable that we can use for an environment conditional
		// We set the environment in index.php: live, dev, or local
		// This setting assumes we set the environment name in our index.php file
		// {% if craft.config.env == 'live' %} ... {% endif %}
		'env' => CRAFT_ENVIRONMENT,

		// Triggers
		// If you wish to access the control panel via a different URL
		// or change your page trigger for pagination, you can do so here.
		'cpTrigger'       => 'admin',
		'pageTrigger'     => 'p',

		// User account related paths
		// Depending on your user needs, you may want to adjust several 
		// of the available user settings to customize your users workflow
		// for logging in, changing a password, or being activated
		'loginPath'                   => 'members',
		'logoutPath'                  => 'logout',
		'setPasswordPath'             => 'members/set-password',
		'setPasswordSuccessPath'      => 'members',
		'activateAccountSuccessPath'  => 'members?activate=success',
		'activateAccountFailurePath'  => 'members?activate=fail',
	),
	
	// Config settings for the LIVE environment
	// ------------------------------------------------------------
	'live' => array(

		// Setting allowAutoUpdates to false will disable the
		// ability to use the one-click update feature.  This might
		// be useful if you are managing a git workflow and want to 
		// ensure that updates happen in your repository first.
		'allowAutoUpdates' => false,

	),

	// Config settings for the DEV environment
	// ------------------------------------------------------------
	'dev' => array(

		// Give us more useful error messages
		'devMode' => true,

	),

	// Config settings for the LOCAL environment
	// ------------------------------------------------------------
	'local' => array(
		
		// Give us more useful error messages
		'devMode'	=> true,
		
		// Have Craft send ALL emails to a single address
		'testToEmailAddress'	=> '[email protected]',

		// Some settings helpful for debugging 
		'phpMaxMemoryLimit'          => '256M',
		'backupDbOnUpdate'           => true,
		'translationDebugOutput'     => false,
		'useCompressedJs'            => true,
		'cacheDuration'              => 'P1D',

		// Make member login settings nice and trusting
		// by allowing a user to stay logged in for 101 years
		// and relaxing various other related settings
		// http://www.php.net/manual/en/dateinterval.construct.php
		'userSessionDuration'           => 'P101Y',
		'rememberedUserSessionDuration' => 'P101Y',
		'rememberUsernameDuration'      => 'P101Y',
		'invalidLoginWindowDuration'    => 'P101Y',
		'cooldownDuration'              => 'PT1S',
		'maxInvalidLogins'              => 101,

	)
);

Level up in Craft CMS with practical examples, snippets, and patterns.
Craft The Planet emails are sent out several times a week.