Introduction To CodeIgniter Framework
CodeIgniter is an open source web application framework for the PHP language. CodeIgniter has many features that make it stand out from the crowd. CodeIgniter will also run in shared hosting environments as it has a very low footprint, yet it still has exceptional performance.
On the programming side, CodeIgniter is both PHP4 and PHP5 compatible, so it will run on the majority of web hosts out there. It also uses the Model View Controller (MVC) design pattern, which is a way to organize your application into three different parts: models — the database abstraction layer, views — the front end template files, and controllers — the business logic of your applications. In the core, CodeIgniter also makes extensive use of the Singleton design pattern. This is a way to load classes so that if they are called multiple times, the same instance of the class will be returned. This is highly useful for database connections, as you would only want one connection each time that the class is used.
CodeIgniter also has an implementation of the Active Record pattern. This makes it easy to write complex SQL queries and makes your application more readable. Active Record also allows you to easily swap and change database drivers. This allows you to write the queries in PHP and still use a MySQL backend, and also gives you the option to switch to an Oracle database –without having to rewrite the queries in your application.
CodeIgniter also comes with a number of highly useful libraries and other sets of functions that help you to build your applications. This leaves you to focus on the small part of your application that is unique, rather than the part that is used across all projects, such as database queries and parsing data.
Introducing Model View Controller (MVC):
Model View Controller — from now on, referred to as MVC — is a software development design pattern. MVC is an approach to separating your applications into three segments: Models, Views, and Controllers. MVC structures your application in this way in order to promote the reuse of program code.
The Model represents any type of data that your application may use. Some examples of data that your application might use would be: a database, RSS Feeds, API calls, and any other action that involves retrieving, returning, updating, and removing data.
Views are the information that is being presented on the screen to users through their web browsers. These are usually HTML files, sometimes containing PHP code that builds the template for the website. In CodeIgniter however, views can be page segments, partial templates, or any other type of page or template.
Finally, Controllers are the business logic of your application. They serve as an intermediary between Models and Views. The Controller will respond to HTTP requests and generate web pages.
However, CodeIgniter’s approach to MVC is very loose. This means that Models are not required. This is for a number of reasons. Firstly, CodeIgniter’s Database Library can be used in both Models and Controllers — meaning that the extra separation of Models can be bypassed. Secondly, the Model isn’t tied to the database, as it is in other frameworks such as Ruby on Rails, so the Model isn’t needed in this regard. Finally, if using a Model in your application will cause unnecessary complexity, then the Model can simply be ignored.
However, Models are extremely useful, even though they are optional. Models can be loaded from any Controller, so if you use a Model function in multiple controllers and you need to change the function, you only need to edit it in one place rather than in all of the controllers. Complex queries should really be put into a Model. A collection of related queries should also be put into a Model as they can be grouped together. This makes your applications simpler, and it allows you to use the functions in any Controller.