The Model, View and Controller (MVC) design has been used widely to develop complex web sites. The key of this design is to separate the data, process and presentation so they can be developed separately and easier to maintain.
A web server was originally just a file server over the network. It became fancier with more functionalities added and people begin to use it for wider range of things. As a result, web sites become complex applications to run on the web server to provide all kinds of information in the form of the web pages.
To create the web pages, especially the dynamic ones, the web application has to go through the following steps in most cases:
- Get the request from the user and decide how to process it.
- Once decided how, it needs to know where and then retrieve the information that the user requested, in most cases today, a database stores this information.
- After getting the information, the it will format it and present it in a certain way, mostly, in a HTML format.
With all these tasks, it becomes a challenge to write the application because you simply don't want to stuff all these codes into one program.
In the old CGI world, a program (in Perl or C) was written to display a web page, and it would include everything from getting the data from a DB and creating HTML page by calling the function like: print "<html>"…
Imagine if a designer wants to make some change of a line of HTML code, he has to do the code change, probably through a developer, to make this happen. Even with the help of some tools or scripts for automatic code generation, we have to come up with some new ways so the development process can be easier and the code can be more robust and flexible.
This is the time when the MVC design comes into play. We separate the code into different groups based on what is does:
Controller handles the user's request. It decides which part of the code should be used to process the user's request. With the controller, you can clearly define the flow of the request handling.
Models defines the info retrieval and processing part. For example, if a user points to the browser to a page for sports news, the Model layer will try to search the database to get the them based on the user's request. For example, if the user wants to see NFL news, the program should use the correct query to find the news that are NFL related.
Views basically finishes the work by formatting the information and present it to the user. In a web application, the view program creates the HTML pages and they are eventually sent to the web browser.
The benefit of the MVC design is that it greatly separate the code based on what it does. With this kind of separation, it becomes easier to manage different parts of the application, maintain the small pieces without breaking the others. It also becomes possible that developers and designers can work on different parts of the application without messing up the other's work.
To implement this type of design, some type of framework needs to be in place to connect the different pieces. A developer can certainly write his own, but there have been several well-written open source project out there for us to use.
In PHP world, the Smarty template engine is a great tool to implement the MVC design.