Codeigniter with Model-View-Controller (MVC) architecture

Codeigniter with the power of MVC

PHP as being open source enjoys a lot of free available frameworks. Some are very huge and some are small. People adopt according to their needs.

I personally like Codeigniter by Ellis Lab due to its splicity and minimal setup time. . Sometime you have very strict timelines and you need Agile or rapid development. Here you feel a need for a framework that could be configure with zero or minimal configuration,structured and complete enough to fulfill your requirements. Codeigniter is suitable in this scenario.

For every framework, it is always considerable to choose best architecture for its organization, extensibility and management. Codeigniter uses Model-View-Controller MVC which is the most popular among all for PHP.

MVC is architecture to control access and customized provided information in desired fashion. Codeigniter comply it very well and developers enjoy coding their applications due to its simplicity and power of MVC.

To implement MVC in PHP, you have to have Object Oriented Programming (OOP) concepts. I will not OOP concepts are because these are out of the scope for this article but I am writing another detailed article on this topic and will release soon.

Something about Model – View – Controller (MVC)

  • Controllers receive all requests from users and call models and views to render the information as directed in the request.
  • Models are modulation for any database or other system. Models do not play role in every request received by controllers. Actually controllers manage model when there is any change occurred in model. Views are not aware of any change in models, its controller responsibility to notify related views about the change.
  • Views are customized output representations, generated as per current models state and request sent to controller.

MVC is controlled and desired separation or abstraction for result outputs. User never has access to models or views, he/she only interact with controllers. Controllers are the component, which received calls, sends commands to models to know about its state, do any mention change in models, call views to tell about changes in models and render customized output for views according to direction provide in the call and current state of models. One controller can call many models and views.
There are many variations in MVC architecture.

MVC in Codeigniter

Codeigniter has very simple directory structure. In application folder, you create your web application. Inside application, controller will go to controllers folder, model to models folder and views to views folder. There are many other folders in application folder for many other purposes.

A big attraction for MVC in PHP is to handle web request smartly with SEO-friendly, User-friendly or clean URL.

Consider following request to Codeigniter web application

cyfal.com/index.php/users/update/97

 

After index.php, first parameter is controller name (users), second is function name inside controller (update) and last one is the parameter (97), which will be provided to the function in the controller.

With the help of some tweaks in your .htacess, you can get rid of index.php in above URL and can get neat and clean URL as follow

cyfal.com/users/update/97

 

But how models, views, controller are accessed in Codeigniter. There are some rules you have to follow to route the web request to correct controller. For the sample URL

  • There must be user.php in application/controllers folder.
  • User.php must have a class named Users (Note he first letter is capital here).
  • User class must be inherited by CI_Controller class( A parent controller class for all controllers).
  • Users class must have public function named update.
  • Update function must have a parameter, to which the last parameter (97) will be provided.

Function name and parameters are optional in URL. If function name is not provided, Codeigniter will take index as a function name by default. The number of parameters function has, the number of parameters you have to provide in the URL and they will be passed to the function’s parameters as they occurred in URL

Codeigniter load any model through model method of load object as follow

$this->load->model('Applicants_model');

After that you will have class level variable Applicants_model ($Applicants_model) which is the object for model class Applicants_model. Now you can call any method from Applicants_model class with the variable you got, as follow

$this->Applicants_model->insert();

To load Applicants_model

 

  • There must be applicants.php file in application/models folder.
  • applicants.php must contain Applicants_model class.
  • Applicants_model class must be inherited by CI_Model class (A parent model class for all models).
  • Applicants_model class must have function named insert.

Same for views, controllers can call views as follow

$this->load->view('applicants_view', $data);

Second parameter is an array of data that will be passed to the view. Second parameter is optional and if it is array it will be extracted and its each key will be available as a variable in called view. Mainly its keys are extracted by PHP extract function.

To load applicants_view

  • There must be applicants_view.php file in application/views folder.

As an architecture overview, any web request received by Codeigniter is directed to the controller specified in web request’s URL. This eliminates the direct access to models and views and make controllers the sole responsible to trigger and control all operations. Then, to handle operations for views and models, functionality is provided to controller to load any model and view for interaction. By loading any model, a controller now has rights to do any changes, get any information or check any expected changes. After that, controller can notify any view about this change, pass information to customize it as needed and render it as an output. Clearly model and views are unaware of each other, controller is the glue, which becomes channel to transit any change between views and models.

Codeigniter with Model-View-Controller (MVC) architecture

Codeigniter with the power of MVC

PHP as being open source enjoys a lot of free available frameworks. Some are very huge and some are small. People adopt according to their needs.

I am very font of Codeigniter by Ellis Lab because sometime you have very strict timelines and you need Agile or rapid development. Here you feel a need for a framework that could be configure with zero or minimal configuration, and structured and complete enough to fulfill your requirements. So, Codeigniter is there to rescue.

For every framework, it is always considerable to choose best architecture for its organization, extensibility and management. Model-View-Controller MVC is the most popular among all for PHP.

To implement MVC in PHP, you have to have Object Oriented Programming (OOP) concepts which is out of the scope for this article. I am writing another article on that and will release soon.

Something about Model – View – Controller (MVC)

I will elaborate MVC implementation in codeigniter but first something about MVC

  • Controllers received all request from users and call models and views to render the information as directed in the request.
  • Models are modulation for any database or other system. Models do not play role in every request received by controllers. Actually controllers manage model when there is any change occurred in model. Views are not aware of any change in models, its controller responsibility to notify related views about the change.
  • Views are customized output representations, generated as per current models state and request sent to controller.

MVC is controlled and desired separation or abstraction for result outputs. User never has access to models or views, he/she only interact with controllers. Controllers are the component, which received calls, sends commands to models to know about its state, do any mention change in models, call views to tell about changes in models and render customized output for views according to direction provide in the call and current state of models. One controller can call many models and views.

There are many variations in MVC architecture. Google it to get it :). Now back to Codeigniter.

MVC in Codeigniter

This article is about codeigniter2.1.3 which is current stable version at this time. Codeigniter has very simple directory structure. In application folder, you create your web application. Inside application, controller will go to controllers folder, model to models folder and views to views folder. There are many other folders in application folder for many other purposes.

A big attraction for MVC in PHP is to handle web request smartly with SEO-friendly, User-friendly or clean URL.

Consider following request to Codeigniter web application

greyinfotech.com/index.php/users/update/97

After index.php, first parameter is controller name (users), second is function name inside controller (update) and last one is the parameter (97), which will be provided to the function in the controller.

With the help of some tweaks in your .htacess, you can get rid of index.php in above URL and can get neat and clean URL as follow

greyinfotech.com/users/update/97

Now a look on how models, views, controller are accessed in Codeigniter. There are some rules you have to follow to route the web request to correct controller. We will take about URL as an example.

  • There must be user.php in application/controllers folder.
  • User.php must have a class named Users (Note he first letter is capital here).
  • User class must be inherited by CI_Controller class( A parent controller class for all controllers).
  • Users class must have public function named update.
  • Update function must have a parameter, to which the last parameter (97) will be provided.

Function name and parameters are optional in URL. If function name is not provided, Codeigniter will take index as a function name by default. The number of parameters function has, the number of parameters you have to provide in the URL and they will be passed to the function’s parameters as they occurred in URL

Codeigniter load any model through model method of load object as follow

$this->load->model('Applicants_model');

After that you will have class level variable Applicants_model ($Applicants_model) which is the object for model class Applicants_model. Now you can call any method from Applicants_model class with the variable you got, as follow

$this->Applicants_model->insert();

To load Applicants_model

  • There must be applicants.php file in application/models folder.
  • applicants.php must contain Applicants_model class.
  • Applicants_model class must be inherited by CI_Model class (A parent model class for all models).
  • Applicants_model class must have function named insert.

Same for view, controllers can call view as follow

$this->load->view('applicants_view', $data);

Second parameter is an array of data that will be passed to the view. Second parameter is optional and if it is array it each key will be available as a variable in called view. Mainly its keys are extracted by PHP extract function.

To load applicants_view

  • There must be applicants_view.php file in application/views folder.

Architecture is, any web request received by be Codeigniter be directed to the controller specified in web request’s URL. This eliminates the direct access to models and views and make controllers the sole responsible to trigger and control all operations. Then, to handle operation for views and models, functionality is provided to controller to load any model and view for interaction. By loading any model, controller is now have rights to do any changes, get any information or check any expected changes. After that, controller can notify any view about this change, pass information to customize it as needed and render it as an output. Clearly model and views are unaware of each other, controller is the glue, which becomes middle-ware to transit any change between views and models.

I want to split this article in two articles, one of MVC and another is for Codeigniter, but then I decided that it should be better in this way.

MVC is architecture to control access and customized provided information in desired fashion. Codeigniter comply it very well and developers enjoy coding their application due to its simplicity and power of MVC.