Skip to content

Instantly share code, notes, and snippets.

@chrisvogt
Last active June 18, 2018 05:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisvogt/5675126 to your computer and use it in GitHub Desktop.
Save chrisvogt/5675126 to your computer and use it in GitHub Desktop.
Example showing how to connect CakePHP database config to AppFog's VCAP_SERVICES environment array.
<?php
class DATABASE_CONFIG {
public $default = array();
function __construct() {
## --- APPFOG --- ##
// Read in the VCAP_SERVICES environment variable, parse the json, and
// check to see if it exists. If it does, use the settings for our default
// database connection.
$vcap_services = json_decode(getenv("VCAP_SERVICES"), true);
if(!empty($vcap_services)) { // database connection credentials
$this->default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => $vcap_services['mysql-5.1'][0]['credentials']['host'],
'login' => $vcap_services['mysql-5.1'][0]['credentials']['username'],
'password' => $vcap_services['mysql-5.1'][0]['credentials']['password'],
'database' => $vcap_services['mysql-5.1'][0]['credentials']['name'],
'prefix' => '',
'encoding' => 'utf8',
);
if ($vcap_services['mysql-5.1'][0]['credentials']['port']) {
$this->default['port'] = $vcap_services['mysql-5.1'][0]['credentials']['port']; // mySQL port
}
} else {
$this->default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'production_database_name',
'prefix' => '',
// 'encoding' => 'utf8',
);
}
## --- APPFOG --- ##
}
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'test_database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment