How to maintain application versions in CI??

As a developer I think, yes it is very useful to maintain application version. Even most of the API’s maintain application versions and its really a very good practice. If you have used Google API then you I think you’ve probably noticed that Google allows us to pass a version parameter for us to tell it which version we’re going to make use of.

In this article (tip) I will show you how we can manage Application versions and this way we can create new versions of our applications while supporting legacy versions to ensure backwards compatibility.

This way, we can use single CodeIgniter installation to handle multiple versions of our application using the same index file and we will be able to choose the version by passing a v parameter in the request.

Steps to work this out:-
1:- Rename application folder to 1.0.0. Since this is the first version of API. After this, we need to rename index.php file, call it 1.0.0.php and change the application_folder line in it. Replace line 75 with:

$application_folder = '1.0.0';

2:- Cool, now our application version is 1.0.0. Well, I think you must be thinking how we will call that 1.0.0.php file instead of index?  No, ofcourse not! so let’s create a new index.php file and add the following code to it.

define( 'API_VER', 'v' );

array_key_exists( API_VER, $_REQUEST ) ? $v = $_REQUEST[ API_VER ] : $v = '1.0.0';

if ( file_exists( "{$v}.php" ) ) {
if ( is_dir( dirname( __FILE__ ) . "/{$v}" ) ) {
require dirname( __FILE__ ) . "/{$v}.php";
} else {
$error = new stdClass();
$error->error = true;
$error->description = 'INVALID_API_VERSION';
echo json_encode( $error );
exit;
}
} else {
$error = new stdClass();
$error->error = true;
$error->description = 'INVALID_API_VERSION';
echo json_encode( $error );
exit;
}

We can define the parameter that we want to choose version. If no version is passed, they by default we will be using 1.0.0 version. If a version is passed, then we need to make sure whether the corresponding file or folder is available for that version. Otherwise, we throw an error.

To add more versions, we’d simply have to copy our 1.0.0 folder and the 1.0.0.php file, rename them according to the version we’re going to create, change the application_folder variable in the file and start working on the next version of our API. When we feel comfortable enough with it, we can make it the version by modifying the index.php file. Done :D.

Please feel free to ask your queries or give your feedback if you think this is useful.

Leave a Reply

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