Jump to content

Need some help understanding MVC


atrum

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/236995-need-some-help-understanding-mvc/
Share on other sites

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 ;)

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.