First, I know there are lot of answers about MVC but I need some more-specific ansewer based on my probably wrong understanding of MVC.

I've already read this very good answer (Explain Model View Controller) but I still need some clarification.

I need to understand if I've understood how MVC works because I'm using it in a kind of MVC framework I'm developing.

  • I have the views that are at the moment just "stupid" templates
  • The models that are "things" that fetch data from database (or other sources) and transform them in arrays passed to the views.
  • The controllers that are mostly ajax javascript scripts and observers for buttons and other user events and PHP scripts that are called by the Ajax scripts when I need to perform server-side operations like SQL updates, inserts etc.

So my "MVC understanding" makes my app works in this way:

I open my index that loads the viewsloader(model), this one loads the contents calling the contentsloader(model) that calls some other script depending by the view the user is requesting.
Then when all the data is loaded in a single array, the viewsloader(model) uses it to render the requested view and passes it to the user.

Now the user can click some button (for example a "view contacts" button) and under the hood my contactslistener(controller) calls the contactsloader(model) to load the contacts list, the array provided by the contactsloader(model) passed trhough the contactslistener(controller) which injects it in the view.


I think I have serious problems with the MVC model, looks like the controllers are doing too different things. Probably I should put the observers/listeners in the views part and use controllers just to load contents provided by the models.

  • Are the models dedicated just to data fetching and organization (for example, fetch from database and put it in array organizing it with the right structure)?
  • Are the controllers thought for handle user events or should I use views for this purpose? Should I use them to query the models and inject the answers into the views?
  • Are the views just stupid templates or should I put there some business logic like event listeners? Following what I've read I should put the entire interface-side scripts here and put only (client/server)-side scripts in controllers. But in this case I've not idea about what a controller should do...
  • 2
    "Ask 10 developers and they will describe 11 different ways MVC should work." Instead, you should try understand reasons behind MVC itself. Like design patterns, SOLID, etc.. – Euphoric Jan 30 '14 at 16:01
  • Among the three, jobs for views are the rarest. – reinierpost Jan 31 '14 at 15:25
up vote 1 down vote accepted

Thanks to Euphoric for the very useful answer, I've got what he means but I've even found a very good article about MVC used besides Ajax.

It's not "the bible of MVC" but I've used it to understand the functions that each part of MVC should do.

This is the chart I've found useful, edited a bit by meself:
enter image description here

I've added the HTTP response and requests from and to static-HTML because using a template engine like Mustache there aren't the problems described in the article I link below:

https://blog.glyphobet.net/essay/153

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.