
October 2008
September 2008
August 2008
July 2008
June 2008
I’ve been using CodeIgniter now for a new project I’m working on called Giftola. CodeIgniter is an MVC framework for PHP. I like CodeIgniter because it’s very lightweight, yet it offers some pre-built functionality to deal with some of the tedium (like form validation) of web development.
I’ve used other PHP frameworks in the past, and I find the way CI organizes files to be very intuitive and things are easy to find.
One problem with using frameworks is that often developers don’t actually utilize all of the features of the framework, often reinventing the wheel. In this post, I’m going to go over a very simple, but useful feature of CI, helpers.
Helpers are functions that are stored in a central location in the CI framework, and then utilized anywhere in the application. I’m going to demonstrate a simple helper I wrote called timestamp. Timestamp formats a date time string for SQL insertion. This helper allows me to ensure consistency across my entire application.
Here’s how they work.
CI has its own helper functions, which are located in:
You can add your own helper functions to the following location:
Your file must be named in the form: helpername_helper.php. In my case, I’m going to create a file called:
where I’ll write a very simple function:
function timestamp(){
return date(”Y-m-d H:i:s”);
}
?>
I use this function most often in my Models, so I’ll load it up here. In this case, I have an event model, where I’ll use the helper quite a bit. So, I’ll load it in the constructor.
function Event_model()
{
parent::Model();
$this->load->helper(’timestamp’);
}
}
?>
Finally, when I want to grab a timestamp, I simply call the function:
A pretty simple example, but hopefully it will get you thinking on how to consolidate functionality that you use often into the helpers structure.
Tags: CodeIgniter, frameworks, php, reference
« Newer Post Older Post »