Open In App

Describe the MVC structure used by CodeIgniter

Last Updated : 07 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Models, Views, and Controllers (MVC) patterns are used by CodeIgniter to organized the files. This helps us to maintain the data, the presentation, and flow through the application. To make things more clear we can understand with their basic definition –

  • Models manage the data of the application and help to enforce any special business rules that the application might need.
  • Views are simple files, with little to no logic, that displays the information to the user that is received through controllers.
  • Controllers act as bridges, marshaling data back and forth between the view (or the user that’s seeing it) and the data storage.

In the most basic way of understanding, controllers and models are simply classes that have a specific job. They are not the only class types that you can use they also make up the core of how this framework is designed to be used. They even have designated directories in the /app directory for their storage of files like controllers, models, views, helpers, config, and many more, though you’re free to store them wherever you desire, as long as they are properly named. To discuss MVC in more detail by elaborating on models, views, and controllers below:

Views: Views are the simplest files which are typically HTML, CSS, Javascript, SVG, and many more related to the frontend with very small amounts of PHP. The PHP should be very simple in views files, usually just displaying a variable’s contents, or looping over some items and displaying their information in a table. Views get the data to display from the controllers and Views are generally stored in “/app/Views”, but can quickly become very hectic to manage if not organized properly. CodeIgniter does not force its use by any type for the views, but a good rule would be to create a new directory in the Views directory for each controller so that dealing would be easy and very clear with the frontend. Then, name views by the method name which makes them very easy to find later on. For example, a contact page might be displayed in a controller named Contact, and a method named contact. You might store the view file for this method in the “/app/Views/Contact/Contact.php” location in their file structure. That type of organization of files and folders works great as a base habit to get into. At times you might need to organize it differently which is not a problem as long as CodeIgniter can find the file that is to be displayed.

Views folder inside the CodeIgniter app folder

Models: A model’s job is to maintain a single type of data for the application this might be users, blog posts, transactions, etc through the methods. So, the model’s job has two major parts and that is:

  1. Enforce business rules on the data as it is pulled from, or put into, the database.
  2. Handle the actual saving and retrieval of the data from the database.

For many developers, the confusion comes in determining what business rules are enforced. It simply means that any restrictions or requirements on the data are handled by the model. This might include normalizing raw data before it’s saved to meet standards and requirements, or formatting a row/column in a certain way before handing it to the controller. By keeping these business requirements in check with the model, you won’t repeat code throughout several controllers or accidentally miss updating an area. Models are typically stored in the “/app/Models” location in their file structure.

Models folder inside the CodeIgniter app folder

Controllers: Controllers have different roles to play but the most obvious one is that they receive input from the user and then determine what to do with it. Controller working:

  1. Most of the time it involves passing the data to a model to save it or requesting data from the model that is then passed on to the view to be displayed.
  2. To handle specialized tasks that are outside of the purview of the model some utility classes are loaded and that is also done by the controller only if required.
  3. It is also responsible to handle everything that belongs to HTTP requests – redirects, authentication, web safety, encoding, etc.

In layman language, the controller is where you make sure that people are allowed to be there, and they get the data they need in a format they can use. Controllers have typically stored in the “/app/Controllers” location in the file structure, though they can use a namespace to be grouped as per the developer requirements.

Controller folder inside the CodeIgniter app folder


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads