Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.