thephpx random header image

Staged Code-Igniter Web Application Development

September 8th, 2009 by admin · No Comments

Today I am going to share a little customization that I do while i develop codeigniter based web application. Now a days in most web application development firms a three-tire development process is followed. This is done to ensure smooth roll-out of the application from development to live.

The three-tire development methodology involves three stages of development servers. First comes the local server which is setup in your laptop or workstation. Then comes the internal test server, which is a central server that holds all the SVN repository as well as hosts the site and make it accessable to all from within the network. This server is also sometimes called staggin server. Last but not the least comes the live server, in most cases its your clients server where you upload your codes after they have passed the QA checks of both local-server and staging-server.

One related aspect of staged development is that in most cases they involve using SVN server for version control. Using SVN, though you can automatically update your server files from SVN server through “svn update” command but if the config file is not configured to take the different servers into consideration you will have to update the config file every time you do update on a server or set separate config file on each server and add the file to ignor list of the SVN repository.

Our way of doing it is to use a single config file and make it lookup the host address to determine which configuration settings to use.  If you are familiar with codeigniter then you already know you will find your config file at system/application/config/config.php.

Find the following line in the config.php file:

  1.  $config[‘base_url’] = "http://localhost/codeigniter/";

Replace the line with the following lines -

  1. if($_SERVER[‘HTTP_HOST’] == "localhost"){
  2.   $config[‘base_url’] = "http://localhost/codeigniter/";
  3. }else if($_SERVER[‘HTTP_HOST’] == "192.168.1.1"){
  4.   $config[‘base_url’] = "http://192.168.1.1/codeigniter/";
  5. }else{
  6.   $config[‘base_url’] = "http://www.live-server.com/codeigniter/";
  7. }

Now all your URL’s will be adjusted based on the server the site is accessed from. Next we will modify the database.php file which is also located in system/application/config/database.php

Find the following line(s) in the database.php file:

  1.  $db[‘default’][‘hostname’] = "localhost";
  2.   $db[‘default’][‘username’] = "root";
  3.   $db[‘default’][‘password’] = "";
  4.   $db[‘default’][‘database’] = "restobolivia";
  5.   $db[‘default’][‘dbdriver’] = "mysql";
  6.   $db[‘default’][‘dbprefix’] = "";
  7.   $db[‘default’][‘pconnect’] = TRUE;
  8.   $db[‘default’][‘db_debug’] = TRUE;
  9.   $db[‘default’][‘cache_on’] = FALSE;
  10.   $db[‘default’][‘cachedir’] = "";
  11.   $db[‘default’][‘char_set’] = "utf8";
  12.   $db[‘default’][‘dbcollat’] = "utf8_general_ci";

Replace the above line with the following once:

  1. if($_SERVER[‘HTTP_HOST’] == "localhost"){
  2.  
  3.   $db[‘default’][‘hostname’] = "localhost";
  4.   $db[‘default’][‘username’] = "root";
  5.   $db[‘default’][‘password’] = "password";
  6.   $db[‘default’][‘database’] = "database_name";
  7.   $db[‘default’][‘dbdriver’] = "mysql";
  8.   $db[‘default’][‘dbprefix’] = "";
  9.   $db[‘default’][‘pconnect’] = TRUE;
  10.   $db[‘default’][‘db_debug’] = TRUE;
  11.   $db[‘default’][‘cache_on’] = FALSE;
  12.   $db[‘default’][‘cachedir’] = "";
  13.   $db[‘default’][‘char_set’] = "utf8";
  14.   $db[‘default’][‘dbcollat’] = "utf8_general_ci";
  15.  
  16. }else if($_SERVER[‘HTTP_HOST’] == "192.168.1.1"){
  17.  
  18.   $db[‘default’][‘hostname’] = "localhost";
  19.   $db[‘default’][‘username’] = "root";
  20.   $db[‘default’][‘password’] = "password";
  21.   $db[‘default’][‘database’] = "database_name";
  22.   $db[‘default’][‘dbdriver’] = "mysql";
  23.   $db[‘default’][‘dbprefix’] = "";
  24.   $db[‘default’][‘pconnect’] = TRUE;
  25.   $db[‘default’][‘db_debug’] = FALSE;
  26.   $db[‘default’][‘cache_on’] = FALSE;
  27.   $db[‘default’][‘cachedir’] = "";
  28.   $db[‘default’][‘char_set’] = "utf8";
  29.   $db[‘default’][‘dbcollat’] = "utf8_general_ci";
  30.  
  31. }else{
  32.  
  33.   $db[‘default’][‘hostname’] = "localhost";
  34.   $db[‘default’][‘username’] = "root";
  35.   $db[‘default’][‘password’] = "password";
  36.   $db[‘default’][‘database’] = "database_name";
  37.   $db[‘default’][‘dbdriver’] = "mysql";
  38.   $db[‘default’][‘dbprefix’] = "";
  39.   $db[‘default’][‘pconnect’] = TRUE;
  40.   $db[‘default’][‘db_debug’] = FALSE;
  41.   $db[‘default’][‘cache_on’] = FALSE;
  42.   $db[‘default’][‘cachedir’] = "";
  43.   $db[‘default’][‘char_set’] = "utf8";
  44.   $db[‘default’][‘dbcollat’] = "utf8_general_ci";
  45.  
  46. }

The above code will give the server specific database configuration to the application based on the server it is accessed from.

Now save these two files and test your application you should be able to use the same config.php file in different servers if they are properly configured on config.php and database.php

For any further query feel free to leave a comment I will try my best to help you out.

take care,

thephpx

Tags: development · efficiency

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment