Skip to content

Instantly share code, notes, and snippets.

@yckart
Last active December 19, 2015 07:18
Show Gist options
  • Save yckart/5917388 to your computer and use it in GitHub Desktop.
Save yckart/5917388 to your computer and use it in GitHub Desktop.
Adding Multiple Web Environments to Anchor CMS - http://abandon.ie/notebook/adding-multiple-web-environments-to-anchor-cms

Step 1. Install Anchor as normal.

Step 2. Create the environments file.

Create a new php file called environments.php save it into the anchor folder and add this content:

<?php
 
// Define Environments - may be a string or array of options for an environment
$environments = array(
     'local'       => array('.local', 'local.'),
    'development' => '.dev',
    'staging'     => 'stage.',
  'preview'     => 'preview.',
);
 
// Get Server name
$server_name = $_SERVER['SERVER_NAME'];
 
foreach($environments AS $key => $env)
{
	if(is_array($env))
	{
		foreach ($env as $option)
		{
			if(stristr($server_name, $option))
			{
				define('WEB_ENV', $key);
				break 2;
			}
		}
	}
	else
	{
		if(strstr($server_name, $env))
		{
			define('WEB_ENV', $key);
			break;
		}
	}
}
 
// If no environment is default to production
if(!defined('WEB_ENV')) define('WEB_ENV', 'production');

Step 3. Include it in the initilisation.

Open the root index.php and just above where it says require SYS . 'start' . EXT; around line 33 add require APP .'environments' .EXT;:

<?php

/*
               /   \
              |  o  |
               \   /
        ________) (________
       |                   |
       '------.     .------'
               |   |
               |   |
               |   |
               |   |
    /\         |   |         /\
   /_ \        /   \        / _\
     \ '.    .'     '.    .' /
      \  '--'         '--'  /
       '.                 .'
         '._           _.'
            `'-.   .-'`
                \ /
*/

define('DS', DIRECTORY_SEPARATOR);
define('ENV', getenv('APP_ENV'));
define('VERSION', '0.9.1');

define('PATH', dirname(__FILE__) . DS);
define('APP', PATH . 'anchor' . DS);
define('SYS', PATH . 'system' . DS);
define('EXT', '.php');

require APP .'environments' .EXT;
require SYS . 'start' . EXT;

Step 4. Edit the database connection.

Open anchor/config/db.php and in place of the database connection info paste the following:

<?php
 
switch (WEB_ENV)
{
        case 'local':
    case 'development':
    case 'staging':
  case 'preview':
		
		return array(
			'default' => 'mysql',
			'prefix' => 'anchor_',
			'connections' => array(
				'mysql' => array(
					'driver' => 'mysql',
					'hostname' => 'localhost',
					'port' => '',
					'username' => 'root',
					'password' => 'root',
					'database' => 'anchor',
					'charset' => 'utf8'
				)
			)
		);
 
		break;
	
	case 'production':
 
		return array(
			'default' => 'mysql',
			'prefix' => 'anchor_',
			'connections' => array(
				'mysql' => array(
					'driver' => 'mysql',
					'hostname' => 'HOST',
					'port' => '',
					'username' => 'USER',
					'password' => 'PASS',
					'database' => 'DATABASE',
					'charset' => 'utf8'
				)
			)
		);
 
		break;
 
	default:
		
		break;
}

Remember to save your current database connection and paste it into he correct place in the switch. You can now add the info for multiple database connections depending on the environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment