What the hell is a “Model-View-Controller”?
The Model-View-Controller Architecture, or MVC for short, is a way of programming that separates your application into three main parts:- the Model
- the View
- and (you guessed it) the Controller
- Business Data (represented by the Model)
- Presentation Data (represented by the View)
- and Application Logic (represented by the Controller)
Using this also makes decoupling and modularization of your application very easy. Since each action on your application will have its own view-controller pair, this pair can be developed separately from another function with little or no setback.
The first time I read about MVC, I was a bit confused about the concept of three separated aspects of my application. I’ve read countless analogies trying to explain the MVC, and for me, one of the best analogies I’ve seen so far was from this article – Another way to think about MVC:
So, let’s imagine a bank.
The safe is the Database – this is where all the most important goodies are stored, and are nicely protected from the outside world.
Then we have the bankers or in programmatic terms the Models. The bankers are the only ones who have access to the safe (the DB). They are generally fat, old and lazy, which follows quite nicely with one of the rules of MVC: *fat models, skinny controllers*. We’ll see why and how this analogy applies a little later.
Now we’ve got our average bank workers, the gophers, the runners, the Controllers. Controllers or gophers do all the running around, that’s why they have to be fit and skinny. They take the loot or information from the bankers (the Models) and bring it to the bank customers the Views.
The bankers (Models) have been at the job for a while, therefore they make all the important decisions. Which brings us to another rule: *keep as much business logic in the model as possible*. The controllers, our average workers, should not be making such decisions, they ask the banker for details, get the info, and pass it on to the customer (the View). Hence, we continue to follow the rule of *fat models, skinny controllers*. The gophers do not make important decisions, but they cannot be plain dumb (thus a little business logic in the controller is OK). However, as soon as the gopher begins to think too much the banker gets upset and your bank (or you app) goes out of business. So again, always remember to offload as much business logic (or decision making) to the model.
Now, the bankers sure as hell aren’t going to talk to the customers (the View) directly, they are way too important in their cushy chairs for that. Thus another rule is followed: *Models should not talk to Views*. This communication between the banker and the customer (the Model and the View) is always handled by the gopher (the Controller).
(Yes, there are some exception to this rule for super VIP customers, but let’s stick to basics for the time being).
It also happens that a single worker (Controller) has to get information from more than one banker, and that’s perfectly acceptable. However, if the bankers are related (otherwise how else would they land such nice jobs?)… the bankers (Models) will communicate with each other first, and then pass cumulative information to their gopher, who will happily deliver it to the customer (View). So here’s another rule: *Related models provide information to the controller via their association (relation)*.
…
So what about our customer (the View)? Well, banks do make mistakes and the customer should be smart enough to balance their own account and make some decisions. In MVC terms we get another simple rule: *it’s quite alright for the views to contain some logic, which deals with the view or presentation*. Following our analogy, the customer will make sure not forget to wear pants while they go to the bank, but they are not going to tell the bankers how to process the transactions.
MVC in Web Development
Currently, a lot of web applications and Frameworks (Zend, CakePHP, Symfony, to name a few) make use of the MVC Architecture. According to the Cookbook from CakePHP:A diagram from ash.MVC, another PHP framework which uses the MVC architecture, illustrates the usual data-flow for a web application built with MVC in mind:Why use MVC? Because it is a tried and true software design pattern that turns an application into a maintainable, modular, rapidly developed package. Crafting application tasks into separate models, views, and controllers makes your application very light on its feet. New features are easily added, and new faces on old features are a snap. The modular and separate design also allows developers and designers to work simultaneously, including the ability to rapidly prototype. Separation also allows developers to make changes in one part of the application without affecting others.
If you’ve never built an application this way, it takes some time getting used to, but we’re confident that once you’ve built your first application using CakePHP, you won’t want to do it any other way.
- First, the browser sends an HTTP request to the web application
- A dispatcher usually will get this HTTP request and determine which controller to be used. This dispatcher is usually called a Front Controller (not illustrated)
- The appropriate controller will receive it and from the data received, determine which model/s are needed, what data is required and what action is to be done to the data
- The model will be told by the controller and retrieve the data and implement any necessary actions. Then, it will return the results to the controller, successful or not.
- The controller, based on the results from the model, will then determine which view should be used and provide the view whatever data is to be published to the browser.
- The view will receive this data, implement any presentation logic to the data, and return it back to the controller.
- The controller will then return the results to the browser.
On a standard web application, the MVC can be represented by the following:
No comments:
Post a Comment