Skip to content

Instantly share code, notes, and snippets.

@jamierumbelow
Created June 17, 2009 16:22
Show Gist options
  • Save jamierumbelow/131330 to your computer and use it in GitHub Desktop.
Save jamierumbelow/131330 to your computer and use it in GitHub Desktop.
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 <br /> 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment