There's a lot of talk these days about design patterns. The most talked about pattern in the realm of web development seems to be the MVC pattern, or the Model-View-Controller. It is this pattern that many of the most popular frameworks are built around. Frameworks like symfony, CakePHP and CodeIgniter enforce this pattern and offer tools to help make using it a breeze. Ruby's famous Rails framework and Perl's Catalyst also make use of the MVC pattern. So what is the big deal? Should you care? In this post I'm going to briefly try and explain the MVC pattern, why you should use it, and how.

What is What

The MVC pattern is made up of three separate parts -- the Model, View and Controller. In this section we'll explore each part and what they are supposed to do.

The Model

The Model is the thing that takes care of the data in your application. It offers ways to find and retrieve data, ways to save data, etcetera. In a lot of cases your model might be a simple class that is just a map of a database table. Many of the web frameworks like symfony give you tools to automatically generate a PHP class that has all of the properties and methods for any particular database schema you have. For example, a users table might have a corresponding User class with methods like User::findByUsername() and User::setEmail().

The important thing is that you encapsulate the domain logic. Sometimes all the model needs to do is retrieve rows from a database or read data from a file. Other times additional processing may be needed, like calculating the users age based on a birth date.

The View

The View takes the data from the Model and displays it in a useful way. In web applications this usually means that the data from the Model is displayed in an HTML page. Some other Views might be for generating an RSS feed or data for a web service. By separating the Model and the View, you can easily switch things up to create new interfaces to the data without much work at all.

Note that keeping the Model and the View separate does not mean that you can't get direct access to the Model itself. In many cases you will be using the Model directly. To output a users username you might use the Model and call User::getUsername() and output it. The idea is only that you separate the domain logic from the presentation. For example, you might have some sort of subscription service. If you wanted to output a message when the users subscription is expired it might seem logical to do something like this within the View:

  1. Welcome back, <?php echo $user->getUsername(); ?>!
  2.  
  3. <?php if ($user->getSubscriptionExpiry() <time()): ?>
  4.     Your subscription has expired. Please renew.
  5. <?php endif; ?>

This is mixing domain logic with the presentation. You are hard-coding what "expired" means directly into the View. If a time came when you needed to change the criteria for when a user is considered expired, then you'd need to go back into every file and change this hard-coded definition. Instead, you should create a method in the Model:

  1. class User {
  2.     // ....
  3.  
  4.     public function isExpired() {
  5.         return ($this->getSubscriptionExpiry() <time());
  6.     }
  7.  
  8.     // ...
  9. }

The Controller

The Controller is the part that connects the Model and the View so they can work together. Each part should be quite separate. That is, the Model should not have any code in it for outputting HTML (or any presentation) and the View shouldn't worry about things like fetching results and calculating user expiration dates. Since the two are completely separate for the most part, you need a Controller in the middle that connects them and makes the whole shebang work.

The Controller takes data such as input and works with it. In web apps this usually means taking information from the URL or forms, processing it, and then outputting a web page as a result. For example, if a user wants to update their email address then the the Controller will take the new email address from the form input, use the Model to update the record in the database, and then show a success page to the user using the View.

Here's how a request might be handled:

  1. A request comes in
  2. The controller looks at the incoming data like the query string or form data
  3. The controller might need to work with the model to fetch the requested data or to update it. For example, using User::findByUsername() to search for a particular user or User::setEmail() to update an email address.
  4. Then the controller invokes the view to output a result. This might be a page with all the information for the requested user, or a success page after the email address was saved.

What Is Each Part

We've explored what each part should do, but you still might be confused as to what each part actually is. Is it a file? A class? What?!

The thing is, the MVC is only a patten. There is no single right way to implement it. A Model is can be a single class that encapsulates all the work needed. But by no means does it need to be a single class. It is not uncommon for a Model to consist of multiple classes. For example, one class that is responsible for encapsulating the data and working with it, and another class that is responsible for actually fetching the data. A lot of the time one Model may depend on another Model to satisfy related data like connecting a web order to a user account that made the order.

The Controller is just a single class at it's most basic form. You might have a single class and each method is defined to handle one type of request (list users, edit user, search users etcetera). But again, it is not uncommon for a Controller to be made up of several different parts.

The View can be almost anything. In web applications it is usually a template that is evaluated with data gathered by the controller. You can use simple PHP files like demonstrated earlier, or a template system like Smarty.

The important part of using the MVC pattern is just the way that all these parts are separated form each other.

Why

Why use this pattern? At first glance it seems like it's a lot of extra work. If you wanted to make a list of users for example, then a simple list_users.php script that does a database query and outputs some HTML seems like it should take 15 minutes. If you use the MVC pattern, you'll have to think about creating the model first, and then the separate template files for the view, and then you have to worry about the controller to glue them together.

All valid points. It is true that the MVC pattern requires a bit more work. But as soon as you have laid down the foundation, it will make your life so much easier. While some changes may require edits in each part, like a dramatic change to the model might need changes made to the controller and view, you'll also get away with other changes more easily (remember our user expiration example?). More then that though, by keeping these three parts separate you allow yourself the possibility to interchange them. For example, using just one single model you can easily create a number of different interfaces to the data. Imagine how easy it would be to create an HTML page, an RSS feed and an XML data provider. In fact, all three of these things might even share a common Controller and only use separate Views. Adding a web service to interact with the data is as simple as creating a new controller to handle requests.

Try the MVC for your next project, you'll be glad you did.

17 Responses to “Intro to the MVC Pattern”

  1. Forrest Says:

    I mostly use the MVC style for Windows applications; it's interesting to see it applied through PHP in a web server context.

    But this (or some variation) is really the only way to build a non-trivial application. What you're describing is a bit of a variation on what I've used, but it seems like a lot of that is the stateless nature of the web.

  2. Danny Says:

    This is a very nice article. I especially liked the last section pertaining WHY should MVC should be used. A good MVC approach can leave your code loosely coupled, and you did a good job explaining that with the subscription example.

    I look forward to more articles by the infamous Nadeau.

  3. Stanis Says:

    I'd like to say that Compass component framework is a good point to start with. It combines MVC and IOC patterns.

  4. Cody Says:

    Haha...by MVC I totally thought you meant motor-vehicle crash. I've never heard of this acronym. Thanks for the explanation!

  5. Jim Says:

    I have read some other articles on the same topic and your article blows them away. I am going to now build all my large applications this way. Great idea. I sort of used it before, but you explained it much better and now my brain just clicked with all sorts of possibilities.

  6. Devlog » MVC With The Zend Framework Says:

    [...] MVC with Zend Framework is quite easy. Here's a brief overview of each part and how they relate to each other. [...]

  7. gabe solomon Says:

    I have been using a similar pattern as an MVC ... just didnt know i was doing this ... somehow every article i read on MVC made me think of this elaborate and complex scripting pattern that was to evolved for me :). Youre article is very well written ... thank you for your article.

  8. murepp Says:

    Техсервис предлагает комплексное обслуживание авто всех марок и моделей. На специализированных площадках созданных на автопредприятии с существованием специализированного инструмента, профессионалы выполнят кузовной ремонт volkswagen. Проводя обозначенные услуги для иностранных марок транспортных средств, достигнут большой опыт, но не только наличие багажа знаний, но практический опыт, дает возможность сделать диагностику автомобилей в намеченный срок и точно определить все ошибки в эксплуатации агрегатов.

  9. vollgeo Says:

    акпп ссанг ёнг мастера установят вследствие технологичной оснащенности сети московских техцентров, а также тому, что полный ряд автоуслуг выполняют высококвалифицированные специалисты, имеют место большие успехи по качеству, формируется отличная функциональность отремонтированных агрегатов и узлов, при этом используются как оригинальные, так и аналоговые автозапчасти. По окончании оговоренных услуг владельцам транспортных средств предоставляется гарантия качества.

  10. natagilix Says:

    Продам сайт!

    Продам сайт USSI.RU есть в сайпе, ussi.ru, ссылки не продавал долгое пора, вчера открыл для покупку. 3-4 кросспостера. 1.5 месяца вспять посещаемость была 150-200, счетчик открыт.

    Одна проблемка: домен не переоформлен, а выделен в аккуант на сайте idomen_ru, действует до ноября(для таких условиях покупался). Связь с бывшим владельцем имеется.

    Забирайте ради 50$.

    ----------------------------------------------------------------------------------------------------------------------------------------------------------

    I inclination offer a milieu!

    I hand down retail a locate USSI.RU is in сапе, ussi.ru, the regard did not sell down the river extensive occasionally, yesterday has opened on purchase. 3-4 posters. 1.5 month ago gate was 150-200, the token is opened.

    One difficulty: the domain is not renewed, and allocated in an account on a position idomen_ru, operates farm November (on such conditions it was bought). Communication with the ex- owner is available.

    Take away as a replacement for 50$.

  11. monxan Says:

    диагностика акпп сааб - входит в производимый современными автоцентрами спектр автоуслуг в неограниченном формате. В зависимости от рода вскрытых неисправностей уточняется метод выполнения диагностики и ремонта.

  12. bubycliccap Says:

    Buy Cheap Viagra Online generic viagra discount cheap |generic viagra zenegra cialis levitra generic prescription viagra without|generic viagra online generic viagra in united states
    generic viagra discount cheap is an vocal drug used to analyse man's impotence. This Generic interpretation of the great’s maiden ED pill should be charmed at least 30-60 minutes before sexy intercourse. It lasts 4 hours and can be enchanted up to three times a day. Generic Viagra has the unvaried physical ingredient as maker prominence Viagra, and is of a piece in effect, incisiveness and dosage.
    generic cialis available in united states is an oral drug used to handling of masculine impotence.This bestselling lozenge offers the longest doing period on the sell, when enchanted 30-60 minutes in the forefront coition it can form up to 36 hours.
    cheapest generic viagra professional is an articulated drug worn to treat masculine impotence. Viagra Dab hand is a supplementary chemically enhanced recipe on an enhanced sensuous sexual intercourse and libido advance . This Generic version of the world’s beginning ED pill should be bewitched at least 30 minutes formerly sexual intercourse. It lasts 36-48 hours. Viagra Dab hand has the word-for-word running ingredient as trade name name Viagra, and is equivalent in effect, stamina and dosage.
    cheap viagra super active is suitable for treating erectile dysfunction in men of any age. Additionally, as it not unexpectedly increases testosterone levels, improves perseverance and boosts bodily intestinal fortitude, it can be safely used past anyone, young and time-worn alike, looking a more effectual sexual congress life, regular if they don't in point of fact suffer from impotence or ED.
    acomplia diet pill rimonabantt is an anorectic anti portliness drug. It is a CB1 cannabinoid receptor antagonist. Rimonabant has been institute to stop rations craving ample supply to support people bow to strain, and could also improve restraint other dangerous urges, such as smoking
    Discount Online Propecia is a one-a-day, FDA-approved treatment quest of manful pattern hair's breadth extermination at the king or in the centre of the scalp (androgenetic alopecia).Generic Propecia has the yet active ingredient as brand name Propecia, and is equivalent in impact, gameness and dosage.

  13. Colombopic Says:

    Ïðåäëàãàåì óíèêàëüíûé áèçíåñ â ñåòè
    Íèêàêèõ âëîæåíèé è çàòðàò.
    çàíÿòîñòü íå áîëåå äâóõ ÷àñîâ â ñóòêè.
    Ñðåäíèé äîõîä îò 25000 ðóá/ìåñÿö.
    Òâîé óñïåõ çàâèñèò òîëüêî îò òåáÿ.

  14. Dutsaugmentes Says:

    Levitra tv fix. Viagra cialic levitra. Cialas vs levitra.

  15. Dutsaugmentes Says:

    Alcohol levitra. Television commercials levitra! http://vimeo.com/levitra Prozac interactions with viagra cialis levitra! Does levitra work. Levitra encyclopedia.

  16. eremeeiii Says:

    Качественное обслуживание транспортных средств зарубежных марок и моделей проводит техцентр. В отделах укомплектованных на автопредприятии сотрудниками осуществляется обслуживание acura, применяя технологичное оснащение. Важные опыт и умение накоплены при осуществлении ремонтных автоуслуг любых марок и моделей а/машин, но не только наличие сведений, а также практический навык, делает возможным точно установить любые ошибки в действии агрегатов и довольно быстро произвести полноценное диагностирование машин.

  17. JustyssQ Says:

    My computer seems totaly fine except for the fact that Google will not work. It will not let me go to http://www.google.com, mail.google.com, google maps etc. It also will not let me sign into Youtube, which i know is related to google's sign in. This has been going on for about two weeks but nothing was wrong before that.

    ______________
    Sharecash methods

Leave a Reply