4

Read tens of articles on MVC, and right now, it's one mess for me. I am trying to do the right thing with my project, which I am rewriting from procedural php into oop, however, I am confused.

I am sure, Model should include all the business logic, which makes sense imo. Not too sure about View. Should I pass data to View, and render the template accordingly, or should the View just ask for data it needs (from updated Model)?

Read a lot about it on SO too, but it just got worse. Answers differ, and I am a bit lost.

What is the right explanation of MVC pattern? (Well, I know it could be different here and there, but the point should stay)

MVC 1

enter image description here

MVC 2

enter image description here

notnull
  • 1,908
  • 3
  • 19
  • 25
  • For avoidance of doubt, are you thinking about ASP.NET MVC or the more general concept of Model-View-Controller? – Philip Kendall Dec 25 '13 at 21:40
  • 2
    What language / platform are you using? MVC isn't possible for everything and is not a magic silver bullet to make your code more X. – PeeHaa Dec 25 '13 at 21:43
  • I try to get it on general level, but I am rewriting web app in PHP – notnull Dec 25 '13 at 21:43
  • In that case it would be impossible to implement the proper MVC pattern fully. – PeeHaa Dec 25 '13 at 21:44
  • @PeeHaa Thanks, thats what I came with after reading a lot about MVC in general. Can you tell, what is the right approach from the two diagrams above? – notnull Dec 25 '13 at 21:49
  • 1
    Option 2 looks sane. Option 1 is how those crappy PHP framework are doing it so they can market something by saying: OMGWTFBBQAWESOME WE DO MVC, while all they are actually doing is using terms like models, controllers and views in a way that has nothing to do with MVC. – PeeHaa Dec 25 '13 at 21:51
  • Instead of articles I suggest you to read a good book. Also there are some more insightful videos, like [Architecture the Lost Years (Robert Cecil Martin; 4 Nov 2011)](http://hakre.wordpress.com/2012/01/09/architecture-the-lost-years-robert-cecil-martin-4-nov-2011/). It's perhaps just time to step a little big and see the big picture again. – hakre Dec 25 '13 at 21:57
  • And as you're concerned about the users-role in this, please see http://heim.ifi.uio.no/~trygver/themes/mvc/mvc-index.html – hakre Dec 25 '13 at 21:59
  • @hakre Good read, thanks. – notnull Dec 26 '13 at 10:11

2 Answers2

1

from my experience writing web application in ASP.NET MVC I can tell you it is great pattern! The Model is used to define your pure entities ( usualy entities that will be stored in a database ).

The Controllers handles user input for example when I first write the url of the site the request coming to the controller and the controller create model object and pass it to the view so the view can be rendered according to the model the controller pass to it.

hope I help you!

ilay zeidman
  • 2,654
  • 5
  • 23
  • 45
0

Imho,... give a ** on this theoretical details. Just do it the way you feel most compfortable but definetly try to seperate concerns as there are:

  1. saving and retrieving raw data
  2. do programatical stuf with the data
  3. present the data

So assuming you have a user table I would create one class containing all the different mysql statements you need to create, edit, delete the users and so on. Then you need templates for example for user details page. It contains only html, css and so on and php variables and maybe some loops or ifs.

The rest of your code uses those two components and serves the result to the client.

Markus Kottländer
  • 8,228
  • 4
  • 37
  • 61
  • This does not answer OP, but it is the correct answer. +1. Much better than trying to cram some paradigm / pattern into something it is not meant for and simply applying common sense. – PeeHaa Dec 25 '13 at 21:56