0

I'm having an issue understanding how to utilize model view controller in the context of my registration system. From my understanding, the view is loaded, showing the user my registration html form. Once the user hits submit, I have a javascript registration class that uses ajax to handle the POST of the data the user entered. My main index.php file has the usual

if(isset($_POST['action'])) {
    if($_POST['action'] == "register") {
        // send the data to the model
    }
}

The model will handle all the database related registration logic, and sends a message back indicating a success or fail. This message is captured by the ajax in my registration javascript class, which updates some of the views HTML to tell the user whether the registration was a success or fail (valid email etc).

My main questions would be why would my view need a reference to the model, like every example i see. And what part of the above registration system is the controller supposed to handle.

Oblivion
  • 585
  • 2
  • 8
  • 26
  • This is very vague. In general, a _view_ would need to reference a _model_ to render itself. – Aluan Haddad Nov 24 '17 at 09:37
  • The controller keeps track of what model works with what view. The view should not directly communicate with the model. This logic should sit in the controller. When the model returns some data, in your case a success or error message, the controller checks what view was registered to this, and changes the view. – Vinod Bhavnani Nov 24 '17 at 09:37
  • MVC wasn't really made with web in mind so any applications of it to websites is subjective (so there is no clear answer to your question). I initially found it very hard to understand it for this reason. I found it more useful to study MVP, which is really closer to what people use when they apply MVC to website apps. My use of it is the Presenter _only_ manages the model and then loads the appropriate view. The view is the HTML, XML, etc. Everything else (such as bootstrap and router) is neither M, V or P and I refer to it as "Application". – texelate Nov 24 '17 at 09:39

1 Answers1

0

The controller in MVC handle user input. They are responsible for taking information, that user passed, and applying it to model layer in order to alter the model layer's state (usually by interacting with various services).

As for the model, saying that "is handles all the database related registration logic" is an actually quite wrong. The data access layer is only a small part of the domain business model (since I am lazy, read more here).

tereško
  • 58,060
  • 25
  • 98
  • 150