Describe the MVC structure used by CodeIgniter
Last Updated :
07 Feb, 2022
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:
- Enforce business rules on the data as it is pulled from, or put into, the database.
- 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:
- 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.
- 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.
- 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
Similar Reads
Describe a CodeIgniter 'model' in detail
In this article, we will discuss the model in CodeIgniter in detail? At the first, let's discuss the MVC structure in CodeIgniter. As we know over decades website has gone from simple HTML with CSS to complex application with hundreds of developers working on them To make working on these complex ap
4 min read
Explain CodeIgniter folder structure
CodeIgniter is an Application Development Framework to build websites using PHP. It is used to minimize the code while developing an application and developed as much fast. The folder structure is an important part of CodeIgniter. It is important to understand the file structure in CodeIgniter to de
3 min read
How to create custom 404 page in CodeIgniter ?
We often see the 404 error page while using websites. This error page will encounter only when we navigate to a broken link. In CodeIgniter, it is possible to create a custom 404 page to change the look of the 404 page. So in this article, we will look into how to create the custom 404 error page.
2 min read
Introduction to Codeignitor (PHP)
Codeignitor is one of the popular MVC framework of PHP. Most of the developers prefer to make their projects on Codeignitor because of it's lightweight and easy to understand documentation. Some of the features, advantages or why use Codeignitor is given below. Why use Codeignitor? Fast and lightwei
4 min read
Database Connection and Queries in Codeigniter
To retrieve data from database we always need to connect our project with database. We will see how to connect database in codeignitor and how to run queries in it. In config folder we can find database.php file to connect with database. Mention the details as per shown image below. database.php In
3 min read
Explain the directory structure in Ember.js
Ember.js framework is designed to be used in large-scale, complex applications that require a robust and maintainable codebase. Ember.js uses the Model-View-Controller (MVC) design pattern, which separates an application into three main components: the model, which represents the data of the applica
6 min read
How MVC works in Codeignitor ?
Codeignitor is based on MVC design pattern. We will see how the controller, model, and view work and display the result to the user. Controller: Like the name suggest, it controls the communication between views and models. All the URL works in CodeIgniter with controller name. Now let's see some po
4 min read
How to set or get the config variables in Codeigniter ?
Config Class: The Config class provides means to retrieve configuration preferences. The config items in Codeigniter can be set and get in the environment. The config value $this->config can be used to get and set items in the environment. The config items are contained within an array, namely, $
2 min read
Folder Structure of a NestJS Project
Organizing a NestJS project with a well-planned folder structure is important for readability, scalability, and maintainability. A clear structure helps in managing code, configurations, modules, and other assets effectively. In this article, we are going to learn the folder structure of a NestJS pr
5 min read
How to set & unset session variable in codeigniter ?
The session class in CodeIgniter allows the user to maintain a userâs âstateâ and track their activity while browsing the website. The session can be initialized using the library and auto-loaded in the environment using the following command. $this->load->library('session'); Set the session v
2 min read