How to use Couchdb NoSQL database on CodeIgniter

Leave a comment

Here I will explain how to use CouchDB NoSQL database on CodeIgniter using PHP On Couch open source library (Data Access Library to access a CouchDB server with PHP).

PHP On Couch tries to provide an easy way to work with your CouchDB documents with PHP.

Here I assume you know PHP and CodeIgniter. If don’t go through http://ellislab.com/codeigniter Its running on your system.

Here is How to use Couchdb NoSQL database on CodeIgniter?

First Download PHP On Couch from here:


Setup instructions:


1.) copy the PHP on Couch classes

copy all from /PHP-on-Couch-master/lib/

to /CodeIgniter_2.1.4/application/libraries/

2.) copy the PHP on Couch CI classes

copy from /PHP-on-Couch-master/frameworks/ci/libraries/

to /CodeIgniter_2.1.4/application/libraries/

3.) copy the config/couchdb.php file inside the config folder of your application/libraries/

copy from /PHP-on-Couch-master/frameworks/ci/config

to /CodeIgniter_2.1.4/application/config/

4.) edit the config/couchdb.php file to set the two configuration options couch_dsn and couch_database :

$config[‘couch_dsn’] = “http://localhost:5984/“;

$config[‘couch_database’] = “my_wonderful_db”;

5.) if you want the couchdb object to be autoloaded, edit the config/autoload.php file of your CodeIgniter application and add “couchdb” to the list of libraries to autoload

$autoload[‘libraries’] = array(‘couchdb’);

6.) Thats it!












How to Enable Site-Wide Profiling in CodeIgniter

Leave a comment

To measure the performance of a web site we need to get loading time of all classes and number of queries and execution time of each query etc.
In CodeIgniter it can be done very easy way. A very good part of CodeIgniter is its ability to give you the ‘profiling’ information for page loads. That is, if you add:


In your controller before you load a view, CodeIgniter will give you information regarding how fast the page loaded, how many SQL queries executed, the content of each query, and the running time of each query. This is incredibly useful when you are trying to debug your application, or simply see how quickly things are loading.

There’s only one problem: To enable profiling, that line of code above must be present. What if you want to profile several pages, or even your whole web application? In that case, you have to start thinking:

I could put that line in the constructor of my controller, and then of of that controller’s methods will be profiled.
I could put that line in each method I want to profile.

These methods start to get ugly. And of course, you don’t want to comment out each profiling line when you don’t need them.

I will suggest to tackle this problem using CodeIgniter’s Hooks feature to enable or disable profiling for the entire web applications based on a value in the configuration file. In the end, I can turn on profiling for my entire website via a config value by setting it to true or false.

Here is the steps:

1. Create a folder in your application directory named hooks (If it does not already exist). Inside it, create a file named hooks.classes.php. Inside it, put this:

class ProfilerHandler
	function EnableProfiler()
		$CI = &get_instance();
		$CI->output->enable_profiler( config_item('enable_hooks') );

2. Create a file in the config directory named hooks.php if it does not already exist. Inside it, place:

$hook['post_controller_constructor'][] = array(
                                'class'    => 'ProfilerHandler',
                                'function' => 'EnableProfiler',
                                'filename' => 'hooks.classes.php',
                                'filepath' => 'hooks',
                                'params'   => array()

3. Finally, enable hooks in your CodeIgniter Application by going to config/config.php end setting the flag to true:

$config['enable_hooks'] = TRUE;	// TRUE or FALSE depending upon your choice, what you want enable/disable profiling

Setting this value to true will enable profiling across your entire website.

That All.


%d bloggers like this: