rick_grehan
Contributing Editor

Fabulous PHP frameworks: CodeIgniter

reviews
Jan 26, 20116 mins

CodeIgniter combines an easy installation with simplicity and flexibility, but is limited in features compared to competitors

Abstract circular bokeh white light gray sliver colors
Credit: Nongnuch_L / Shutterstock

CodeIgniter, a PHP MVC (model view controller) framework sponsored by Ellis Labs, touts itself as a solution for those developers who eschew large, over-abundant frameworks. CodeIgniter’s installation requires little effort; you unzip the download package to your Web application’s home directory and modify a pair of files — one a general configuration file, the other governing database specifications. CodeIgniter needs only PHP version 4.3.2 or later. (The CodeIgniter engineers decided not to require PHP 5, because they did not want to alienate current users already employing PHP 4.) Perhaps CodeIgniter’s only downside is its lack of JavaScript support, although this is expected in a future release.

Like the Zend Framework, CodeIgniter uses the front-end controller pattern. All HTTP requests are received by index.php, sent to the proper processing routines through a routing system, passed through a security system (which shields the application’s controllers from malicious input), then finally handed to a controller. The controller loads the model, processes the request, and sends the result to the view system.

CodeIgniter helpers, libraries, extensions
Though it has a small footprint, CodeIgniter is extensible through a variety of mechanisms: helpers, libraries, class extensions, and hooks. A helper is a collection of functions that handle related tasks. CodeIgniter includes a sizable collection of helpers: for preloading input forms with data taken from arrays or objects, for JSON (JavaScript Object Notation) and the Mootools JavaScript library, for managing digital media, and more. By comparison, a library provides more extensive behavior than a helper. CodeIgniter has libraries for user login and authentication, session management, encryption, file uploading, and so on.

A class extension is — as its name implies — simply the PHP mechanism for extending the instance variables and methods of an existing class to provide specialized capabilities. Finally, CodeIgniter hooks let you alter the behavior of the framework itself, without having to hack into the code. You define a hook by specifying a hooking point. For example, the pre_system hooking point attaches the hook early in the process of system execution, while the post_system hooking point lets you inject behavior after the Web page has been rendered. You then associate class and method, as well as the file path to the class and method that you want invoked. When the application’s execution hits the hooking point, CodeIgniter will dynamically load and execute your code.

Test Center Scorecard
 
 40%25%25%10% 
CodeIgniter 1.7.38989

8.4

Very Good

CodeIgniter supports the MVC pattern loosely. You can, for example, employ only controller and view components if no back-end database (and, therefore, no model) is required. CodeIgniter uses the active record database pattern for database access, and the framework’s database access methods are straightforward and easily mastered. For example, a simple get() method looks like this:

$query = $this->db->get('theTable');

This retrieves all the data from table theTable. You can modify this in a fashion mimicking an SQL WHERE clause where the second argument is an associative array that specifies the fields participating in the WHERE conditional:

$query = $this->db->get_where('mytable', array('id'=>$id, $limit, $offset);

CodeIgniter even provides methods that let you assemble more complex queries by chaining together comparison operations joined by AND and OR logical operators.

CodeIgniter’s database connection information is stored in a multidimensional array. This allows you have multiple sets of database connection specifications — say, one for development, another for testing, and so on. You can easily switch from one database to another by changing a single, global variable.

Keeping CodeIgniter simple
CodeIgniter installation is drag-and-drop simplicity. Download and unzip CodeIgniter, and you’re provided with an application directory tree that you copy into your application server’s Web home directory to begin your application’s development. CodeIgniter has no command-line tools, nor does it use a templating language. Both omissions exemplify CodeIgniter’s principle of simplicity.

In the case of the absent command-line tools, CodeIgniter’s engineers wanted to minimize the number of entities supplied by an installation. CodeIgniter does not use a templating language for the simple reason that — again, according to CodeIgniter’s engineers — PHP is already a templating language, so why create another? CodeIgniter documentation claims that, rather than try to learn PHP and a templating language, it’s better to master PHP. In addition, code written in a template language must be processed and translated to PHP at runtime or converted to PHP in a separate build step — there is a performance benefit to avoiding an independent template language.

CodeIgniter provides security on a number of levels. It restricts the characters that it will permit in an URL. Also, because it uses the “segmented model” for URLs it accepts, CodeIgniter simply disallows HTTP GET operations. In addition, CodeIgniter provides security libraries. You can, for example, call upon a CodeIgniter library to establish an XSS (cross-site scripting) filter that CodeIgniter will run on any incoming data.

CodeIgniter’s documentation can be found on the website’s wiki, and it includes a user guide and tutorials. There are two “official” video tutorials; the rest are tutorials linked to on external sites, and the number of those is remarkable. Not only are there “getting started” videos, which cover AJAX and managing uploads, you’ll also find numerous entries written on topics such as database manipulation, using views, integrating email, and more. The user guide itself has information on installation and getting started, as well as topic overviews, class references, and a reference for the system’s available helpers.

Though some of the documentation is under development, the quantity of information linked to on external sites more than makes up for the lack. CodeIgniter’s installation is a breeze, and although it does not have all the bells and whistles of some of the other frameworks, it is an excellent framework to get a Web application started quickly.