atrum Posted May 20, 2011 Share Posted May 20, 2011 Hello all, So I am trying to become familiar with the concept of the MVC (Model View Control) design pattern. I think I get the concept but I tend to need an example in its most basic practical form to understand something well enough to actually make use of it. I was wondering if anyone could provide an example and an explanation with out a lot of theory. (I have plenty of books on the theory is why but very little on real life application). Thanks, Atrum Quote Link to comment https://forums.phpfreaks.com/topic/236995-need-some-help-understanding-mvc/ Share on other sites More sharing options...
ignace Posted May 20, 2011 Share Posted May 20, 2011 MVC is a simple pattern. It's so simple that most programmers already use it without knowing. A possible Controller could be: if ($_SERVER['REQUEST_METHOD'] == 'POST') { mail($_POST['to'], $_POST['subject'], $_POST['message']); } include 'contact.html'; This little piece of code has a high separation of concern (which MVC promotes). The View (function include() in my example) knows how to parse PHP/HTML and pass the output to the browser. The Model (function mail() in my example) knows how to send an e-mail using the appropriate protocols. The Controller responds to requests and delegates the necessary work to the Model and the View (retrieve the form, send an e-mail). A simple example for a simple concept Quote Link to comment https://forums.phpfreaks.com/topic/236995-need-some-help-understanding-mvc/#findComment-1218191 Share on other sites More sharing options...
atrum Posted May 20, 2011 Author Share Posted May 20, 2011 Man I love getting replies from you Ignace. You know how to speak my language. I never even imagined that something I have been doing pretty much from day 1 is the MVC pattern. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/236995-need-some-help-understanding-mvc/#findComment-1218194 Share on other sites More sharing options...
ignace Posted May 20, 2011 Share Posted May 20, 2011 The key here is abstraction. The View can be many things among HTML, XML, JSON, CSV, or even plain text. The same goes for a database. If you ask about databases you'll get answers like MySQL, PostGreSQL, SQLite. Some alternative answers would be Cassandra and CouchDB. An answer you'll never hear but is equally valid is a webservice. Most webservices provide the same CRUD operations any normal DB provides: PUT (Create), GET (Read), POST (Update), DELETE (Delete) or better known under the acronym CRUD. Quote Link to comment https://forums.phpfreaks.com/topic/236995-need-some-help-understanding-mvc/#findComment-1218196 Share on other sites More sharing options...
atrum Posted May 20, 2011 Author Share Posted May 20, 2011 Thanks again man, this makes a lot more sense now. Yet another example of how my brain can over complicate something that is in actuality really simple. Quote Link to comment https://forums.phpfreaks.com/topic/236995-need-some-help-understanding-mvc/#findComment-1218206 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.