I am going through many projects and did implement many project architecture for those. From my experience, I found that MVC architecture is best for developing web-related project. I am here talking only about web applications since I am associated with web based projects.
There are several frameworks available which use MVC architecture. The best and easiest method to implement MVC architecture is to use a framework which is implemented with the architecture. You can also create an application which follows MVC architecture.
Here, I’m talking about MVC, but what is MVC? MVC is the abbreviation of Model View Controller. So you need the explanation about each of the words… I am giving you the explanation about that also:
- Model part is associated with the Database. Group of data related modules is collectively called the Model of a project or an application. The model part also specifies the properties and functionality of data used by the project. The model is isolated from other two parts(View and Controller) and linked directly with DBMS/FILE system.
- This part associated with the Input and Output of the project. Input forms and HTML outputs are generated by the View. Generally, view generates the IP/OP and passes it to the controller. The controller then communicates with the user and render the IP/OP passed from View.
- Controller is the part which controls the application. This part communicates with the user using the application. The other two parts(Model and View) operates isolated and passes their result to the controller. The controller then processes the results and passes it to the user associated with the controller. For a web application, the controller is connected to the user through web server and browser.
Here is a block diagram of MVC architecture for a Web application. On this image, MySQL is the database and it is associated with the Model.
How can you create your own MVC application?
- Divide the application into three parts – Model, View, and Controller.
- Write methods/functions for all SQL operations used on your application. This part/file is your Model. You can use Model functions on the controller to get/set data.
- Make a directory named template or any other. Create files with extension .html or .tpl which includes all HTML codes for the site. This part is your view section for the application. The better way to create the view is to use template engines like Smarty.
- Then comes the last, but not the least part Controller. This page is simply the script file (here assuming PHP scripting). The controller includes all model and view files and then uses those two files accordingly. The important point to be noticed while creating controller is that there will not be any output command used in the controller. The only time when controller uses the print command is when the controller outputs the generated view, that is to be the last statement for an execution.
That’s all. You can also simply create an application with MVC structure. There are many benefits for following this architecture. Implementing better architecture on your project helps you to improve performance, cutout development cost on modification and testing phases, easy to debug, etc…