Develop CakePHP in 3 Locations – HOME TEST LIVE

Written by James McDonald

September 7, 2016

I have a CakePHP application that I develop on my MacBook and then push to a “TEST” install environment and then across to a LIVE environment when I’m happy.

In the past I’ve done the following at each instance after I have git pull | fetched | merged:

  1. Edit APP/Config/core.php and change debug, TRUE to FALSE
  2. Edit other  Configure flags
  3. Change the database connection
  4. Hope I’ve remembered to do all of the above

However,

You can set things up to automatically configure to each environment.

This assumes you are using Apache and can make edits to the apache configuration or can add things to .htaccess and the Apache webserver is configured to allow the Overrides.

Firstly in the highest  .htaccess file in your CakePHP install make the following changes:

# The .htaccess file in your LIVE environment
SetEnv ENVIRONMENT LIVE

# The .htaccess file in your TEST environment
SetEnv CAKEPHP_DEBUG 2
SetEnv ENVIRONMENT TEST

# The .htaccess file in your HOME environment
SetEnv CAKEPHP_DEBUG 2
SetEnv ENVIRONMENT HOME

 

Next make sure you have setup your database.php to allow for each environment

class DATABASE_CONFIG {
    // default and 2 work apfoods/ and test/
    public $default = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => '10.20.30.1',
        'login' => 'liveuser',
        'password' => 'liveuserpassword',
        'database' => 'livedb',
        'prefix' => '',
            //'encoding' => 'utf8',
    );
    public $test_pallets = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => '10.20.30.1',
        'login' => 'testuser',
        'password' => 'testpassword',
        'database' => 'testdb',
        'prefix' => '',
        
            //'encoding' => 'utf8',
    );
    
    // 3 and 4 home laptop
    public $home_pallets = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'homeuser',
        'password' => 'homepassword',
        'database' => 'homedb',
        'prefix' => '',
        'unix_socket' => '/tmp/mysql.sock'
            //'encoding' => 'utf8',
    );
    public $home_pallets2 = array(
        'datasource' => 'Database/Mysql',
        'persistent' => false,
        'host' => 'localhost',
        'login' => 'homeuser',
        'password' => 'homepassword',
        'database' => 'homedb2',
        'prefix' => '',
        'unix_socket' => '/tmp/mysql.sock'
            //'encoding' => 'utf8',
    );

}

In APP/Model/AppModel.php you add some code to switch environments

    // default db connection is default
    public $useDbConfig = 'default';
    
    
    function __construct(){
        parent::__construct();
        
        $env = getenv('ENVIRONMENT');
        
        $db_connections = Configure::read('datasources');
        
        $db_connection = $db_connections[$env];
        
        if ($db_connection){
            $this->useDbConfig = $db_connection;
        }
        
        
    }

 

In APP/Config/core.php add some code to automatically set debug mode  which gives you better errror reporting while developing in CakePHP

// use SetEnv CAKEPHP_DEBUG 2 in .htaccess
// this only checks if CAKEPHP_DEBUG is defined
// and then sets debug if it exists
	
        if (getenv('CAKEPHP_DEBUG')) {
            Configure::write('debug', 2);
            Configure::write('pallet_print_debug',true);
        } elseif {
            Configure::write('debug', 0);
            Configure::write('pallet_print_debug',false);
        }
// this maps the Apps SetEnv ENVIRONMENT values to 
// the Cake database.php db connection values
  Configure::write('datasources', [
            'HOME' => 'home_pallets',
            'TEST' => 'test_pallets',
            'LIVE' => 'default'
        ]);

a

 

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

You May Also Like…

Squarespace Image Export

To gain continued access to your Squarespace website images after cancelling your subscription you have several...

MySQL 8.x GRANT ALL STATEMENT

-- CREATE CREATE USER 'tgnrestoreuser'@'localhost' IDENTIFIED BY 'AppleSauceLoveBird2024'; GRANT ALL PRIVILEGES ON...

Exetel Opt-Out of CGNAT

If your port forwards and inbound and/or outbound site-to-site VPN's have failed when switching to Exetel due to their...