5 Things You May Not Know About CodeIgniter
Every time I start a new project with CodeIgniter I find myself tasked with something, often a small thing, that I’ve never “solved” before. I try to use these moments as opportunities to explore PHP and CodeIgniter a little bit deeper. Often times I surprise myself by learning something new, or surprise myself at having forgotten something relatively “basic”. Here are 5 things I’ve discovered or rediscovered in the last few years. If you’re a CI veteran, some of these will be old news to you, and some might be new. Maybe just like me you’ve forgotten about a handy trick. Regardless, they’ve become mainstays in my coding now, and I think they deserve a bit more recognition.
1) There is an optional second parameter in $this->uri->segment()
uri_segment() is one my most frequently used functions to move information from page to page. After all, how many times have you seen http://example.com/robots/futurama/bender vs http://example.com/robots/futurama/clamps in action? In these cases, we often use
$robot = $this->uri->segment(3);
There is an optional second parameter that permits you to set your own default value if the segment is missing. For example, we could use
$robot = $this->uri->segment(3, 'calculon');
This allows you to grab a specific robot out of the url, but if there is no specific robot, simply default on Calculon.
2) Any library can have a configuration file
I try to keep my library configuration options out of my controllers where possible. Fortunately CodeIgniter lets us seamlessly move that information into application/config without missing a step. Simply name your configuration file the same as the library and you’re set. My favourite for this trick is email library. Here’s the application/config/email.php file I include in every bit of code I distribute.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
* If you are using this app in a professionally hosted environment, you probably don't need
* to do anything to this file. If you are running it from your home server, or have a setup
* on a pre-configured server setup, such as WinLAMP or XXAMP, then you'll likely
* need to uncomment the configuration information below.
*
* Check with your ISP for this information. It is usually given in the welcome information
* when you sign up for the service, or can likely be retrieved from your email package of choice.
*/
/*
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'smtp1.mailserver.ca';
$config['smtp_user'] = 'username';
$config['smtp_pass'] = 'password';
$config['smtp_port'] = '25';
*/
/* End of file email.php */
The third parameter allows you to suppress errors in the event that a config file does not exist:
$this->config->load('blog_settings', FALSE, TRUE);
3) remove_invisible_characters()
Here’s a handy little guy.
This function prevents inserting null characters between ascii characters, like Java\0script.
Until recently, I’d forgotten all about this little Common Function. (edit: fixed another link, thanks Ossama)
Generally speaking, you’ll probably be using XSS protection in any place you need this (in fact, CodeIgniter’s xss_clean() function actually calls remove_invisible_characters()), but if you find yourself in a situation where XSS cleaning isn’t warranted, then remove_invisible_characters() might be the way to go.
4) CodeIgniter 2 allows for libraries to have drivers
This isn’t really an example of “oh hey I forgot about that tip” so much as it is an awesome new CI2 feature that isn’t getting the recognition I feel it deserves. The documentation could use a little fleshing out still, but check out Using CodeIgniter Drivers and Creating Drivers. (edit: fixed those links… thanks Adam)
5) View’s can be returned as data with a third parameter
I love this option and can think of two places I’ve used it with great success; building PDFs and constructing downloads.
$page = $this->load->view('view', $data, TRUE);
$pdf = build_pdf($page);
// OR
$this->load->helper('download');
$view_data = $this->load->view('xml_template', $data, TRUE);
force_download($data['page_title'].'.xml', $view_data);
I’m always impressed at the flexibility of my tools, and here are 5 admittedly simple tricks that have given me more “bang for the buck” over the years. What are your favourite tips? Please leave them in the comments!

Eric Barnes wrote on
Just to add one more to the list :) config_item(‘string’); is one I seemed to have overlooked until it was recently pointed out to me. In the docs it is hidden away but very useful to use in your view files.