CodeIgniter 3.0 and How I use it!

So, we recently upgraded our framework version from 2.2.3 to 3.0.0 at The latest version is more around bug fixes and making the session library more secure. There is a huge debate these days over other frameworks, mostly Laravel vs CodeIgniter. Majority of the people are of the opinion that CI is now the granny PHP framework and we should be moving towards younger and better frameworks. But I’ve been a big fan of CodeIgniter from the beginning and don’t want to lose hope all that soon.

Screen Shot 2015-05-28 at 9.49.44 AM

So after upgrading CI to 3.0, I decided to change some of my practices so that I gradually move towards a more modern style of coding. Here are some of my changes:

1. config_item() : This function was available in the previous version as well but I only found about it after upgrading. ( from the up-gradation steps ) I think it is a very handy shorthand for $this->config->item() function. Although the official user guide says that the latter is the preferred method over the earlier, but it also says that there is no harm in using it for retrieving single key values. So unless we are dealing with multiple config files with name conflicts, we should be fine.

2. [] for array definition : This shorthand is pretty handy in my opinion. ( not exactly a 3.0 change ). Traditionally in documentations and online tutorials, arrays have been used using the array()  syntax for both indexes and associative types. But in my opinion a

$temp = [ '1' => 'apple', 'two' => 'orange']


$arr = ['mumbai', 'delhi'];

are much more easier to type. The only limitation here is that this syntax was introduced in PHP 5.4. So as long as we are using a PHP with a higher version, we should be good.

3. $config[‘reuse_query_string’] = true : This new config value in the pagination library in 3.0 has been a huge help. So the way pagination links work in CI is that they append the offset to the url itself. So page 2 of the url will be where 40 is the number of items you show on each page and url for page 3 will be and so on. But if my page has some query params which I want to retain, like for the url :, the pagination url will ignore the GET params, like so : But with the above config variable this is now possible.



CodeIgniter Cache library for Redis

So I’ve been trying to setup a Redis library inside CodeIgniter3.0. My first attempt was to search on git hub which landed me on joelcox’s repo. It seemed promising with enough number of commits and contributors, but as it said it has been deprecated, I had to look further. Now since Codeigniter also provides a Caching library out of the box, I decided to give it a try.

As illustrated in the documentation fairly well, to initiate the cache on needs to write :

$this->load->driver('cache', ['adapter'=>'redis']);

The only difference is the additional array argument that I passed to driver() without which I was not able to get it to work. The redis config needs to be put in a file at location application/config/redis.php. Mine looks like this :

$config['socket_type'] = 'tcp'; //`tcp` or `unix`
$config['socket'] = '/var/run/redis.sock'; // in case of `unix` socket type
$config['host'] = '';
$config['password'] = NULL;
$config['port'] = 6379;
$config['timeout'] = 0;

This is enough to get it to work, and for simple key value pairs the following two statements can be used to set and get the value :

$this->redisObj->save($key, $value, null); // save the data with infinite TTL

$this->redisObj->get($key); // get the value of the key

This is pretty handy. Although it does not provide more redis functionalities like increment or selecting all keys ( keys * ), it does the job for basic key setting/getting requirements. Looking further for a more feature rich Redis Library for CodeIgniter.