MVC Model-View-Controller
MVC Model-View-Controller. MVC a type of coding or programming architecture for developing software or we can say software architectural design pattern. Recently pretty popular in web applications development.
In other words, the three things are very easy.
- Model is what interacts with the database, it would be the back-end class code for an object-oriented language like PHP, Ruby on Rails, or C++.
- View is basically the user interface.
- Controller is the logic that operates everything in between.
In beaning when I started programming I use procedural concept to code. You know how that was, one thing leads to another with a thrown in function here and there and you have a working application. As I grew as a programmer, I came to know that this way of programming is fine for small projects but when I started to develop bigger applications my code became very unorganized and difficult to read and understand. To overcome this I started writing my own classes to help me write better, reusable code that I could use in all my applications. Then I realized that although I was learning a lot by doing this, I was rotating the same wheel again and again. Mostly I was writing had already been accomplished many times over in various PHP frameworks. I started looking into choosing from huge varieties of PHP frameworks and saw most of them were also based on something called MVC.
Learning to program using MVC was a complete new era for me, but it was a worthy struggle. If any one wish to develop applications with readable, well-structured, easy to understand code so that you can quickly rectify the problems, then MVC is for you. Also for different programming languages MVC pattern is best suitable. I use PHP to code and the combination of MVC with PHP made my life easy.
This image will describe the basic flow of MVC
In any application langauge MVC will flow like below
- User communicates with the View which shows a web form.
- User submits the form, and the Controller receives the POST request. It passes this data to the Model.
- The Model updates and queries the database and sends the result back to the Controller.
- The Controller passes the Model’s response to the View.
- The View updates itself with the new data and is displayed to the user.
Lets talk about the advantages of using MVC architecture for coding
Separation of Logic: Using MVC architecture we are separating the Business Logic from Presentation Logic which simplifies the code, code will be easy to understand and maintain.
Debugging is easy: For larger application it was very pathetic debug the code with procedural code. With MVC architecture as we have separated the code it is very straight forward to debug / troubleshoot the code.
Life easy for front end/UI developer: Most of the time UI developer finds it very uneasy to write HTML CSS code with huge amount of PHP(or any other language) code. But as with MVC they have view portion specifically form them, means no business logic are present in View files which makes the view files containing only HTML/CSS code and partially PHP(or any other language) results only to display.
The Controller, in object oriented PHP, will likely consist of several classes that aggregate data from the Model and turn it from relational data into object oriented data. This way the material in the database is translated into usable objects for the actual application layer. Other classes should form the logic layer, providing the 'guts' of the Controller, by guiding interaction within and between the objects. Results of these interactions should then be passed to the View.
Most popular approach: Most of the Frameworks Like Codeigniter, Cake PHP, Zend, Yii, are using MVC pattern.
Object Oriented approach: We can extensively use object oriented approach with MVC which gives all the advantages of OOPS concept.
Efficient modularity of the design allows any of the components to be swapped in and out as the user or programmer desires - even the model! Changes to one aspect of the program aren't coupled to other aspects, eliminating many nasty debugging situations. Also, development of the various components can progress in parallel, once the interface between the components is clearly defined.
Multiple view support: Due to the separation of the model from the view, the user interface can display multiple views of the same data at the same time.
De-coupling: MVC facilitates de-coupling the application's layers.
Conclusion:
Any framework for PHP or for any other language using any design pattern to make the code more easy to
understand, Debug or for code re-usability. As far as PHP is concern most of the frameworks are using MVC architecture as base pattern.
We must be aware though that when we talk about patterns in the programming world, we are talking about flexible frames, to be altered to the particular problem we have. We will find implementations introducing variations on the structure we have seen, but the important thing is that, in the end, the pattern helps us gain a clear division between responsibilities and better maintainability, code-reuse, and testing.
We have also seen the advantages of using an MVC framework that provides us with a basic MVC skeleton and a lot of functionality, improving our productivity and making the development process easier.
Thanks for reading!