Auto-detect environment based on specific server environment variables, and allow per environment configuration and environment emulation.
Based on http://www.developpez.net/forums/d834850/php/bibliotheques-frameworks/cakephp/configurer-plusieurs-environnements-application-cakephp/ and on the work of Rafael Bandeira (environment.php).
- Put
app/Config/environment.php
in yourAPP/Config/
directory. - Create the
environments
folder in yourAPP/Config/
directory and putapp/Config/environments/envs_list.php
inside. - Edit
envs_list.php
according to your available environments. (See example below). - Edit your existing
APP/Config/bootstrap.php
and append the content ofapp/Config/environments/bootstrap.php
to it. - For each environment, edit the highest level .htaccess and append the content of given
.htaccess
. (See example below). - You're done! You can now create your config files according to the example given in
app/Config/database.php
. (See example below).
env_list.php
Environment::configure(
'development'
,array('server' => 'development')
,array('debug' => 2, 'security' => 'low')
);
Environment::configure(
'production'
,array('server' => 'production')
,array('debug' => 0, 'security' => 'high')
);
.htaccess on your development server
SetEnv APP_ENV "development"
.htaccess on your production server
SetEnv APP_ENV "production"
database.php
var $development = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'dev_host',
'login' => 'dev_login',
'password' => 'dev_password',
'database' => 'dev_dbname',
'prefix' => '',
'encoding'=>'utf8'
);
var $production = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'prod_host',
'login' => 'prod_login',
'password' => 'prod_password',
'database' => 'prod_dbname',
'prefix' => '',
'encoding'=>'utf8'
);
var $default = array();
function __construct()
{
if (isset($_SERVER['APP_ENV']))
$this->default = $this->$_SERVER['APP_ENV'];
else
$this->default = $this->development;
}
function DATABASE_CONFIG()
{
$this->__construct();
}
Licensed under The MIT License.
Copyright (C) 2013 Maxime ALAY-EDDINE
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.