Now learning: CodeIgniter
Lately I’ve been spending most of my time on various PHP frameworks, such as CakePHP, symfony, CodeIgniter, and Prado. After spending sometime with all of them, I’ve decided to go with CodeIgniter. When people talk about frameworks first thing that comes into mind is Ruby on Rails. RoR has changed the whole web application development scenario and the way web applications are being developed. Frameworks make programming more fun by reducing lots of repetitive tasks.
CodeIgniter is a toolkit for building PHP based web applications. Its goal is to enable you to develop projects much faster than you could if you were writing code from scratch, by providing a rich set of libraries for commonly needed tasks, as well as a simple interface and logical structure to access these libraries. CodeIgniter lets you creatively focus on your project by minimizing the amount of code needed for a given task.
Like most other frameworks, CodeIgniter(CI) uses M-V-C approach which allows separation between logic and presentation. So you can use models to store all database related functions, Views to put regular html codes which generates contents for end users and then use Controllers to bind Models and Views.
So, why did I choose CI?
- It is extremely well documented. You don’t expect people to use a framework without giving them proper documents and information on how to use it.
- It does not require any command line interaction. That’s what happened when I tried to use Symfony.
- No restrictive coding rules (almost) unlike CakePHP which has too much naming conventions to follow. For example, table name has to be plural and lowercased, model names are singular and CamelCased and so on. The list is ridiculously long. Bye bye CakePHP.
- Easy to learn (thanks to through documentation) and simple syntax.
Though CI does not have AJAX and authentication module, it can be implemented using third party plug in. Hope next release comes with native support of AJAX and authentication module.
A simple example:
Say I have a table called bookmarks with fields: id, name, url and I want to display all the bookmarks as an unordered list. First I’d create a model named bookmark_model.php with code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Bookmark_model extends Model { function Bookmark_model() { parent::Model(); } function get_bookmarks() { $query = $this->db->get('bookmarks'); return $query->result_array(); } } ?> |
the function get_bookmarks() gets data from database table and returns as an array. Everything in two lines and without writing any SQL syntax!
Next I’ll create a controller named bookmark.php with following code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); class Bookmark extends Controller { function Bookmark() { parent::Controller(); } function Index() { $this->load->model('bookmark_model'); $data['bookmarks']= $this->bookmark_model->get_bookmarks(); $this->load->view('bookmark_view', $data); } } ?> |
Here in the Index() function, we are loading bookmark_model, then the array of values from bookmark_model is stored in $data array and finally we are loading View called bookmark_view and passing the $data array.
Finally I will create bookmark_view View with code:
<html> <head> <title></title> </head> <body> <h2>My Bookmarks</h2> <ul> <?php foreach ($bookmarks as $bookmark):?> <li><?=anchor($bookmark['url'],$bookmark['name'])?></li> <?php endforeach; ?> </ul> </body> </html> |
As you can see it’s a simple html page with php loop. From the bookmarks array we assign anchor tag to url field and name as title. Pretty simple. So as you can see controller acts as middle man and binds view and model together.
We call a web page using class and function name. So, if my website is http://www.clearpixels.net, to run the above code we have to point to http://www.clearpixels.net/bookmark/index. The Index() function will be executed and bookmark_view will be populated with bookmarks list.
So, If you know little PHP (like me), not willing to spend a lot of time in learning a framework, go for CodeIgniter. You will love it. Follow this link for a crash course. It is extremely well written and easy to understand.
-
July 3rd, 2008 at 6:37 pm
Great post! It’s Mike from the CapsizeDesigns tutorial you plugged. Just wanted to say thanks for reading and I look forward to some more good ones from you :).
[Reply]
-
July 4th, 2008 at 2:08 pm