I work with PHP and understand how MVC works. There is one thing I am not sure about MVC. Is it a good practice to create a Model object directly in the View without passing it through the Controller because sometimes there is no need to process the Model in the Controller? Is there any disadvantage of doing that?
-
3I think you are making a mistake... You need to learn about MVC properly. [How models should be structured](http://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc/5864000#5864000) – IROEGBU Nov 24 '14 at 09:37
-
`because sometimes there is no need to process the Model in the Controller` - Although this might be true, a model is almost always able to alter it's own state. The view-layer is not the right place to let that happen. – thpl Nov 24 '14 at 09:37
-
You might benefit from reading [this answer](http://stackoverflow.com/a/16596704/727208) – tereško Nov 24 '14 at 11:45
3 Answers
Its against the concept of MVC
Don't try to break the Architecture
Model - The lowest level of the pattern which is responsible for maintaining data.
View - This is responsible for displaying all or a portion of the data to the user.
Controller - Software Code that controls the interactions between the Model and View.
Reference Here

- 1,855
- 6
- 20
- 38
Your view
only displays results, so new Model()
must go to controller and later be passed to view.

- 41,402
- 5
- 66
- 96
Yes - your view shouldn't need to rely on the Model staying the same.
Say for example, you're using a model which has ->forename
and ->surname
attributes. In your view, you just call these directly.
Then you decide later that you're going to add mutators and accessors (getters and setters) to your model, so you'll be using ->getForename()
and ->getSurname()
because you want to do some pre-processing on them to ensure the capitalisation is correct.
Now you need to go through all your controllers and views, because the usage needs to change.
If you'd instead just done all the model processing in your controller and then passed a standardised set of data to the view, you'd only need to update your controllers.
A View shouldn't expect anything of the Model, it should just require that it gets specific data from a Controller.
You could do something like this in your Controller:
$view = new View('my.file', [
'user' => [
'forename' => $user->forename,
'surname' => $user->surname,
],
] );
this still gives you a $user['forename']
to use in your View, but now the format of that data is coming from the Controller, not the Model.

- 15,669
- 4
- 48
- 83
-
Good answer. People don't understand that the biggest benefit of MVC is scalability. Ruin that and you might as well not use the pattern. – Alternatex Nov 24 '14 at 10:54
-
"Model" is not an object. Thus the premise of your answer is wrong. What you end up with is a leaking domain logic in the controller. Also, "view" is not a tempalte. – tereško Nov 24 '14 at 11:40
-
@tereško I never mentioned the words "object" or "template" in my answer. I used object syntax for demonstrating how you might access data from the model **as an example**, but I really don't understand your comment and downvote – Joe Nov 24 '14 at 11:44
-
I was referring to your code example. What you have there called `new View` is a template (with filename and assigned values). And all of what you write about "model" is basically a [domain object](http://c2.com/cgi/wiki?DomainObject). I guess that's what you get when learning about "MVC" from php frameworks ... – tereško Nov 24 '14 at 11:48
-
1@tereško it's a sample implementation of a magical, non-existant MVC framework which is loosely based on how existing PHP frameworks handle Views. Go look at them if you don't believe me, but this isn't the place to rage about how PHP doesn't do things properly. I personally don't believe MVC translates well to PHP and web applications, but if you're strictly using Models, Views and Controllers **and nothing else** (as the question implies), try and give me a better way of doing it – Joe Nov 24 '14 at 11:57