MVC and HTML forms handling

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
nektarios
Forum Newbie
Posts: 2
Joined: Sat Mar 15, 2008 4:15 pm

MVC and HTML forms handling

Post by nektarios »

Hi,

I have developed my own framework which I consider to be good and it is in production for several of my web apps. It's fully OOP but it's not fully MVC and I don't like that, so I branched it to try to make it to a full MVC and also make it be programmer-friendly as possible. And I got to some problems and I have some questions, with how should I brake down my framework logic. That why I need your expert advice.

My main question is with how to handle form submitting/correcting/reloading. But before I get into that let's see if I got the whole concept right.

This my the order of the classes the handle a request:

1. Bootstrap
2. Router
3. Controller
4. Model
5. View

If I understand correctly, firstly the Bootstrap will receive the request and spawn a router that will dispatch it accordingly to the appropriate Controller.

Is this Correct?

Then the Controller responsible will process the request (GET or POST) and figure out what to do with it and which Action to call. The Actions (class methods) reside within the Controller.

e.g.

Code: Select all

 
Controller->ActionAddItemForm();
 
Correct?

Through these Actions (methods) the Controller will spawn the appropriate Models and call some of their methods (e.g. ModelItems->AddNewItem(); ). When this action finishes, then the Controller spawns and calls the View to render the output.

Correct? Or should the Model call the View for rendering? What MVC dictates here?

The View must have different methods for displaying different things? Like for instance an html table, or a div with the contents of the Model?
e.g.

Code: Select all

 
View->compileHTMLTable(); and in the end View->RenderWholeHTMLPage(); 
 
Or these are a responsibility of the Model, or Controller and the View is only responsible for the end rendering of the whole page? e.g.

Code: Select all

 
$HTMLTable = Model->compileHTMLTable(); 
View->Add($HTMLTable); 
View->RenderHTMLPage();
 
I'm confused with all of these, because in theory it seems simple, but in practice there's much confusion of the various responsibilities of each of Model, View and Controller objects.

The above problems are more profound when I deal with Forms. In particular, I can't figure out which object is responsible for building up the form and it's fields, which one should process the POSTed variables and attach them to according fields, which one should validated them, and which one will act upon sucessfully or unsuccefull submission.

The MVC examples I find googling about, they are almost useless because most of them have seriously gotten wrong the whole paradigm.

Thanks in advance!
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: MVC and HTML forms handling

Post by Darhazer »

I didn't see a web implementation of MVC in which the model change the view, and I don't see anything wrong with that. So your MVC architecture is correct - controller interacts with both model and view (although some people call this model view interface).

As for the last question:
View usually have different templates. The Controller gives the data to the view (or the model does), and tells the view which template to load (or you can just have different views for each template). No need for methods. And yes, the view renders the table. The view is responsible for presentation, no matter if this a HTML table, an XML / RSS / SOAP, or even PDF or image. So one Controller / model should be able to represent the web page as XML or PDF just switching the view.

Hope this is helpfull.
Post Reply