CodeIgniter and the code I didn’t have to write

I finally got around to taking CodeIgniter for a spin and putting it in fifth gear. The amount of code I didn’t have to write, and the speed at which I was able to pump out a fully working web application is beyond amazing. Form handling, data validation, session management, authentication, database communication and much more was a breeze. Going from writing your own code and building your own logic for some of those aspects, to using a PHP web application framework that handles all of this is one helluva big step up.

In the past, I’ve fiddled around with CakePHP and slightly got my feet wet with Symfony and I must say, I was impressed. Only recently, I embraced an MVC (Model View Controller) way of life. It sets and defines clear boundaries on the type of code you can write and where. The Model is the code you write to interact with the database. View is code written for displaying your content. Controller is the logic that ties it all together between the View and the Model. I caught myself a couple times writing controller code within the model.

Example of Controller Code

class Customer extends CI_Controller {
	
	public function __construct() {
		parent::__construct();
		//Loading the Customer Model so we can interact with database
		$this->load->model('customer_model');
	}

	public function create_customer() {
		//Create a customer array and pass in values to the
		//create method in the model. Field names in the database
		//table are specified as keys in the array here.
		$customer_data = array(
			'first_name' => "Gary",
			'last_name' => "Raymond",
			'company' => "The Man on the Visa Card",
			'email_address' => "garyraymond@visa.com",
		);
		return $this->customer_model->create_customer($customer_data);
	}
}

Example of Model Code

class Customer_model extends CI_Model {
	
	public function __construct() {
		$this->load->database();
	}
	
	public function create_customer($customer_data) {
		//Specifying the table that customer information will be saved.
		//Pass in array received from controller
		return $this->db->insert('customer', $customer_data);
	}
}

All in all, it’s a great framework and has already simplified many of the tasks I’ve been working on. The documentation is superb and the resources are on point. If you’re not into learning a new way of coding, or a templating language, but want to take advantage of a rich set of libraries, I recommend you take CodeIgniter for a spin.

All of the other frameworks I mentioned are just as amazing. I’ll post a more indepth piece on CodeIgniter and eventually, the other frameworks (if I get around to using it).

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>