CodeIgniter’s Hidden Gems

Written by: Jamie Rumbelow

Jamie is a vibrant, eccentric developer from Cambridge in England. A PHP and CodeIgniter Guru, a fanatic about JavaScript and mad about Ruby on Rails, Jamie spends most of his days trying out new web techniques or talking about them at conferences, in workshops and around the web. He runs his own podcast, writes for his blog and various other publications and releases open source code, libraries and tutorials to the community. In early 2009 he joined forces with Elliot Haughin to setup BinaryCake, a small boutique media publisher focusing on selling high-quality, professional and affordable screencasts. 

I’ve been programming with CodeIgniter for nearly two years now (gosh, is it that long?), and still I keep on finding little treasures buried deep within the framework, long standing there just waiting for someone to discover them.

I’ve compiled a nice, long list of CodeIgniter’s hidden secrets full of little helpful snippets, concealed functions and long-unused classes to help you get the most out of the framework.

The Database Forge

CodeIgniter’s database class goes much further than the simple ActiveRecord system. It’s an extremely powerful tool, and includes the database forge class. This is rarely used – many seem to skip over this documentation section and onto the more promising ActiveRecord class, but this can be really helpful – especially if you need to create, edit and delete actual databases.

You can load the database forge class using a variant of the standard CodeIgniter loading convention.

$this->load->dbforge();

And then call options on it by using the “dbforge” variable.

//Create a database called 'users'
$this->dbforge->create_database('users');

You can find out more about the database forge class at the user guide page, found here.

auto_typography();

One of the most useful functions, and yet often unheard of inside CodeIgniter is auto_typography(), residing in the seldom used typography helper. This helpful function converts double-line breaks to paragraphs, single line breaks to
tags, quote tags to typographically correct curly quotes and apostrophes, double dashes to em dashes and a few other things to make your text semantically and typographically correct.

It’s usage is simple:

//Load the typography helper
$this->load->helper('typography');

//Convert some text
$converted_text = auto_typography($original_text);

It takes two parameters, the string to convert and a boolean value stating if to shorten more than two line breaks down to two. This defaults to FALSE. This is a great function, but can be pretty processor intensive, especially when you are parsing large quantities of text, so it’s a good idea to cache your pages.

CodeIgniter Page Caching

Which brings me perfectly onto my next point, page caching! A post made last year by one of my CodeIgniter all-star brethren (term coined by Michael Wales, circa 2008.), Elliot Haughin benchmarks different caching mechanisms and proves that CodeIgniter’s basic caching solution deserves much more credit then it gets.

CodeIgniter’s caching solution is primitive at heart, but works extremely well and is very easy to set up and use. It’s often unused because people go for other alternatives such as Memcache or APC, both great caching systems, but CodeIgniter’s is fine when all you need is simple page caching.

The caching system is built into the output class, which is automatically instantiated by the system, so you don’t need to load it. There is only one function required to set the cache – and the system handles the rest. You can place it anywhere in the controller function you want to cache – the order is irrelevant. You simply pass the function the number of minutes you want to cache the function for.

$this->output->cache(60); // 60 mins = 1 hour

Combine CodeIgniter’s solution with a caching system such as Memcache, APC or EAccelerator, and you can greatly improve your web application’s speed. Read more about CodeIgniter’s caching system, yet again on the user guide.

url_title();

Favourite of blogging engines CodeIgniter-wide, url_title(), a wee function hiding in the depths of the URL helper takes a string and converts it into a URL friendly string. This is commonly used when creating a slug for a page – it will turn the string “CodeIgniter is Awesome” into “codeigniter-is-awesome”, “Woo! Mama! Hot-diggedy Damn!” into “woo-mama-hot-diggedy-damn” and so forth.

send_email(); & CodeIgniter’s Email Class

If you want to send an email, using basic settings via PHP’s mail() function, you can use this sweet little function. Simply pass it the recipient, the subject and the message and it will send the email. This is great if you want to send a quick log message for instance, but if you need more flexibility and power you should check out the Email library.

This posts latest comments

  1. [...] post: CodeIgniter’s Hidden Gems | codr.eu Share and [...]

  2. Great functions! url_title() is really something I could use ;)

  3. Prabhjeet says:

    i like url_title();

  4. Eren says:

    page caching in codeigniter has a problem when you post a form to the same url resulting in showing the same form without using post value. caching should be disabled for any action page to retrive post data.

Tell us what you think !

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>