The Model-View-Controller architectural pattern has existed for decades. It isn't the only architecture but it sure is the most popular one. Let's dig in and see why it is the preferred pattern for web applications today and how we can make a clean-coded application with this pattern.
- [Voiceover] Practically every popular PHP framework today uses the MVC paradigm. CodeIgniter implements the MVC pattern with the following flow. An entry point, which is index.php, routing, and security. Subsequently, the controller takes over and it manages what goes where, and when. It manages the data from the models to be injected back into the view, and returned to the browser. The model is an interface between our application, and a data repository. It can be a database, or an API, or some other source of data.
Business logic is guarded here. The view is every element that ends up rendered on the browser. Most of the time, HTML is the case, but we may also see XML, or even JSON format, in case an API was being built, or any other format that will either be rendered or generated on the fly. Example, a PDF file. The controller is the leader, or more precisely, the decider. It has the power to establish whether requests will go straight into the view, with cache, or whether it needs some information from the model to be passed into the view.
Finally, while this is not part of the MVC pattern, per se, The entry point is the fundamental part of the MVC implementation of most PHP frameworks. CodeIgniter is not the exception. CodeIgniter has an index.php file as its entry point, and a unique route generator with the format index.php slash controller, slash method, slash parameter one, slash parameter two, et cetera. We can always remove the index.php, so the routes look smoother, but that's really up to you.
Most web frameworks nowadays use either the MVC paradigm or at least some variation of it. It's important to be aware of the things we should avoid, such as doing everything in the controller, or having logic in the view.
- Loading views and data from containers
- Routing with controllers
- Creating models
- Configuring the data
- Working with data, forms, sessions, and headers
- Logging and error logging
- Extending CodeIgniter to consume dependencies