You must be wondering why I am discussing a framework instead of discussing something related to the php language. Well, the answer is that I just started using a framework and found it extremely fascinating. I thought I should share my excitement through this blog so that it might be useful to others who are planning to use a framework in their php application development. The framework that I will be discussing here is called Code Igniter, a well known framework among php developers. The version of Code Igniter discussed here is 2.0.3. The learning curve is not high even for those who are new to php. Yes, we have to agree that they have a good documentation consisting of an introduction, general topics, class reference, helper reference etc etc. I am sure once you start using Code Igniter you will get addicted to it for life. Reason – the unique advantages that Code Igniter has compared to other frameworks. Some of the advantages that Code Igniter has over other frameworks is that Code Igniter includes features like – being light weight, fast, generates clean urls, uses the concept of model-view-controller(mvc), is extensible, does not require a template engine and of course good documentation.
From my experience after using Code Igniter is that Code Igniter is fast compared to the same application written in pure php. In fact, more than speed it is the ability of Code Igniter to cut down the number of simple bugs that impressed me the most. In a team, as everybody is forced to use a common set of rules belonging to Code Igniter, developers get time to focus on the application rather trying to write bug free syntax and usages of php. Yes, Code Igniter uses the mvc pattern. The mvc pattern found in Code Igniter divides the application code into three parts – model part, the controller part and the view part. Every file of an application written using Code Igniter has to belong to the model, view or the controller part. The flexibility of Code Igniter lies in the freedom to ignore the model part and build application using the controller and views alone. Code Igniter also allows the user to include their own scripts and libraries. Thus, Code Igniter is extremely flexible as far as structuring your application is concerned.
The application flow of Code Igniter looks like this: After installing Code Igniter you will see a file called index.php in the Code Igniter’s root directory along with other directories called ‘application’ and ‘system’. This file called index.php serves as the front controller. The job of index.php or the front controller is to initialize the base resources needed to run Code Igniter. So, any request to your application written using the Code Igniter framework has to first pass through index.php. The request reaching index.php is then passed to a router which decides what to do with this request. Router first checks the cache. If a cached copy for the request is available then the cached copy is delivered to the browser else the request is handed over to the security part of the Code Igniter. The security part filters the user’s request for any user submitted data and hands over the request to the controller part of the Code Igniter once the controller is loaded. The controller then loads model, libraries, helpers, plugins etc needed for that request. The loaded model,libs, plugins, helpers etc process the user request and the response is sent back to the controller. The response is then sent to the view by the controller. The finalised view is then sent to the browser (a copy of the view is cached if caching is enabled).
Let us now discuss the design and architectural goals of Code Igniter. The goal with which Code Igniter was designed was to have maximum performance capability and flexibility in the smallest, lightest possible package. Code igniter achieves this by dynamic instantiation, loose coupling and component singularity. Code Igniter achieves dynamic instantiation by loading components and executing routines only when requested. Coupling is the degree to which components of a system rely on each other. In case of Code Igniter less components depend on each other making it more reusable and flexible. Hence Code Igniter can be considered as a loosely coupled frame work. The last strategy that Code Igniter uses to achieve its design goal is by component singularity. By component singularity, it means that in Code Igniter each class and its functions are highly autonomous in order to allow maximum usefulness.
Next, let us take a look at the features available in the Code Igniter frame work (These features are from the code igniter user guide).
The features are:
- Model-View-Controller Based System
- Extremely Light Weight
- Full Featured database classes with support for several platforms
- Active Record Database Support
- Form and Data Validation
- Security and XSS Filtering
- Session Management
- Email Sending Class. Supports Attachments, HTML/Text email, multiple protocols (sendmail, SMTP, and Mail) and more
- Image Manipulation Library (cropping, resizing, rotating, etc.). Supports GD, ImageMagick, and NetPBM
- File Uploading Class
- FTP Class
- Data Encryption
- Full Page Caching
- Error Logging
- Application Profiling
- Calendaring Class
- User Agent Class
- Zip Encoding Class
- Template Engine Class
- Trackback Class
- XML-RPC Library
- Unit Testing Class
- Search-engine Friendly URLs
- Flexible URI Routing
- Support for Hooks and Class Extensions
- Large library of “helper” functions
As you can see there are many wonderful features available in Code Igniter. One feature that fascinated me was Code Igniter’s support for Active Record Database Pattern(ARDP). It uses a modified version of ARDP called the Active Record class. This pattern allows information to be retrieved, inserted or updated to the database with minimal scripting. Some times only one or two lines of code is all that is required to perform a database action. for example:
To select all the records from a ‘student’ table, all you have to code is:
Of course you have to configure the connection details in the config file before you execute this query.
Another feature that I liked about Code Igniter is the division of the code into model-view-controller. Every file that you use in your application should actually belong to either the model, view or the controller.Yes, Code Igniter does provide the ability to write your own libraries and your application code can be part of your own library too. There are a lot more features and one has to take a test drive to understand the power of Code Igniter.
So, to summarize what I have said so far – Code Igniter is a toolkit for people to build web applications in php without having to write everything from scratch. Code Igniter helps you achieve this by providing a number of libraries. According to me Code Igniter is a powerful framework that can be picked up even by those who are new to php.
References: The Code Igniter User Guide