Describe the MVC structure used by CodeIgniter
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:
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.
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.
Please Login to comment...