Setting up Environments in CodeIgniter

May 20th, 2009 by ScottK | No Comments | Filed in CodeIgniter

I will profess that I only a month old in CodeIgniter, a framework written in PHP, I’m certainly no stranger to MVC frameworks. My work with CodeIgniter has been download the clients site, do the work and upload to clients site. Unfortunately this also means reconfiguring the app for my dev box, then hopefully remembering to change those back before I deploy.

God forbid I forget the database change configs. >.<

So with that said I’m introducing how to set up running environments with CodeIgniter so you can work in development settings and then deploy to staging, or any other you specify, without needing to change configurations. Plus you get the advantage of having the ability to modify behavior in your app based upon environment.

Note: I hacking the core of any app, but one file must be changed, but it’s the heart and soul. So in the root index.php file I’ve added: just before the final

/*
|—————————————————————
| LOAD THE FRONT CONTROLLER
|—————————————————————

raman amplifier$http_host = str_replace("www.", "", $_SERVER['HTTP_HOST']);
$environments = array("development" => "localhost",
                      "production" => "techraving.com"
                     );
$found = false;
foreach ($environments as $k=>$v) {
    if (!$found) {
        if ($http_host == $v) {
            define("MY_ENV", $k);
            $found = true;
        }
    }
}

if (!$found) {
    define("MY_ENV","development");
}

So that the heart of setting up the environment. It’s detecting the host on which you are running on and defines the MY_ENV variable for use throughout the application. It’s he only hack to the core, I promise.

The next change would be to add an entry to the CodeIgniter autoload.php. You need to add the environments.php file the autload config section… or:

$autoload['config'] = array('environments');

Now create the environments.php file in the config directory as well as an environments folder. In the environments.php add this:

<?php
include_once("environments/" .  MY_ENV . ".php");
?>

In the environments folder you need to create the different files for each environment you specify in the index.php. So in my case I have to set up a “development.php” and “production.php” file. You can certianly add different files as your $environment specifies.

So what do you do with the different environments? Well here’s what I’m doing:

environments/production.php

<?php
$config['base_url']    = "http://www.techraving.com/";
$config['log_threshold'] = 1;
error_reporting(0);
$config['cookie_domain']    = "techraving.com";
?>

and in the environments/development.php

<?php
$config['base_url']    = "http://localhost/";
$config['log_threshold'] = 4;
error_reporting(E_ALL);
$config['cookie_domain']    = "localhost";
?>

So when I’m developing locally I don’t have to change any configs when I deploy!

What else is there? Well let’s look at the database issue in the config/database.php

$active_group = MY_ENV;
$active_record = TRUE;

$db['production']['hostname'] = "localhost";
$db['production']['username'] = "";
$db['production']['password'] = "";
$db['production']['database'] = "";
$db['production']['dbdriver'] = "mysql";
$db['production']['dbprefix'] = "";
$db['production']['pconnect'] = TRUE;
$db['production']['db_debug'] = TRUE;
$db['production']['cache_on'] = FALSE;
$db['production']['cachedir'] = "";
$db['production']['char_set'] = "utf8";
$db['production']['dbcollat'] = "utf8_general_ci";

$db['development']['hostname']   = "localhost";
$db['development']['username'] = "";
$db['development']['password'] = "";
$db['development']['database'] = "";
$db['development']['dbdriver'] = "mysql";
$db['development']['dbprefix'] = "";
$db['development']['pconnect'] = TRUE;
$db['development']['db_debug'] = TRUE;
$db['development']['cache_on'] = FALSE;
$db['development']['cachedir'] = "";
$db['development']['char_set'] = "utf8";
$db['development']['dbcollat'] = "utf8_general_ci";

So as long as I’m not developing on the production site, I’m NOT working with production data!

By setting the MY_ENV varible there are so many other uses for detecting your enviroment.

Like for instance, only in production should emails be sent to the actual person, otherwise sent them to a defined email address. Yes I’ve seen it happen where developers accidentaly send email to users while developing. And boy do the users get confused; if not angry when this happens.

So by setting up enviroments we can ensure some safety as developers that we won’t be messing up production databases or causing existing users any distress. Plus as developers we don’t have to waste any time making config changes or worring about reverting them when we deploy the new code.

PyMigrate is Listed on Softpedia.com

May 20th, 2009 by ScottK | No Comments | Filed in News

I know it’s been awhile since I’ve made a post; getting ready to launch a major application as well as the paying job. ;) I have felt the blogging itch, especially since I’ve been working exclusively with the CodeIgniter framework. But today a rather exciting email came in.

I want to give a big shout out to the editors who reviewed my PyMigrate library at http://mac.softpedia.com/get/Developer-Tools/PyMigrate.shtml and included it in their Mac section listings. Other than a few comments here early on I haven’t heard anything good or bad otherwise, so this was great news to me.

Big thanks to Softpedia.com!

100% Free award granted by Softpedia

PyMigrate 1.0 Stable Release

March 7th, 2009 by ScottK | No Comments | Filed in PyMigrate

I’ve released the first stable version of PyMigrate today. This release actually detaches all the database API’s to SqlAlchemy. So you will need SqlAlchemy and the associated eggs for your database per SqlAlchemy.

You can find the downloadable versions here online casinoPyMigrate Downloads and the svn repo backgammon free casino money free craps game play free black jack craps video poker strategy play black jack online how to win video poker casino game online uk best casino online casino secure online gambling jackpot casino online casino black jack learn to play craps how to win at video poker craps online blackjack casino game online casino betting free on line video poker casino games no download casino online gambling casino play free casino slots video poker machine bonus video poker free on line slots double bonus video poker free video poker games free casinos roulette online craps rules free on line casino rules of craps online casino free money blackjack 21 internet casino how to play craps free casino game download fortunelounge online casino free casino download free casino card game free roulette game free casino play no deposit free money casino internet casino online here. The supported database API’s (for now) are:

Mysql
Sqlite
Oracle
DB2
Postgres
Oracle
MSSQL

While SqlAlchemy may lack some things, such as dropping an index, the PyMigrate app accomplishes this through a straight sql statement. Granted some of the databases do not support some actions, such as rename colum in Sqlite.

PyMigrate helps all of us quickly change up/down our databases, but I know from a dba stand point that we need “total” control of a few things. So I made the Sql block read multiple lines, instead of one line. That gives you absolute control of your database migration as you need, and still allows you to revert it as needed.

So PyMigrate offers common modifications to tables/indexes/colums, etc. Using the sql: block will the most control you can have.

I’m happy to release this version and will soon be adding more database API’s. Feel free to comment!