MVC Structure - Need feed back

Hi guys,

I really need someone to take a quick look at this mvc project that i am working on… I am learning as I go and I really want this to be used for one of my medium site soon to come. Anyways I have posted this at a different site a day or so ago but nothing yet (yeah I don’t mean to be some what impatient). I also asked on facebook but that is how i got here :slight_smile: thought it is a forum type of help. anyways I have attacked the files to the post… I just need to know if I am doing everything correct, and if there is anything that i should change.

Looks pretty good to me (though i couldnt find where you keep your views?) atleast a lot better then what 99% of the mvc applications out there look like hehehehe.

The only thing i dont get is why your models extend the controller.
The whole purpose of creating seperate models and controllers is to keep their responsibilities seperated. by extending your controller in the model you give the model responsibilities that it should not have.

I learned mvc from this place: http://www.moock.org/lectures/mvc/ although i have made some changes to my code along the way this is a pretty good starting point to devide the responsibilities between the models, views and controllers. and i like it because they also explain it nicely with some schema’s.

I second what brense said.

dbm, what you call “models” in your app are actually controllers.

This may help … http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html … this shows, step by step, how to go from flat PHP to an MVC architecture. (Once you get to the heading “Add a Touch of Symfony2,” you can stop if you choose.)

I now have a new version of this mvc, I am still trying to learn and understand as I go, I have read/viewed other examples as well… the only thing that still gets me is how others do there mvc… each mvc is different to others and what i mean by that is how certain controllers and model are setup… anyways… here is the new one that I am working on. I am trying to do it my own way… but I just want to make sure that I am doing stuff correctly, I really dont want some day for someone to see this and say “wtf!!” hehe… also thanx all for there replies!

Wow perfect website! thanx!

Jeff, yeah I have that one booked marked I also checked them out in github.

Sorry, no time to look at your files right now but I do want to say something else.
There is no true guideline for MVC other than that MVC was designed to split responsibilities in your application between different types of classes (MVC also has many different names).

Generally speaking the models have the least responsibilities. The models are only used to define “entities” like “Person”, “Page”, “Car”, “User”. The controllers are in control of these models. In most mvc frameworks the controllers create instances of the models but I do not completely agree with this. I think the View is the most important thing in mvc. The view should handle user input and output. Once user input is received the view should call a function/method in the controller based on the type of input received, the controller then changes the model accordingly (for example when a user has updated their email address) which is then relayed back to the view which will display the changed model.

A lot of frameworks have html code in their views. This is WRONG. In my opinion you should always have html in seperate template files and have your views parse these templates (fill them with variables from the models for example).

Generally speaking the models have the least responsibilities

On the contrary, that is common misconception (one I made as well). The model carries the bulk of the load…validation, business logic, formatting, etc. This is commonly referred to fat models/thin controllers:

The models are only used to define “entities” like “Person”, “Page”, “Car”, “User”

I think the general opinion today is they encapsulate entire domains. I tend to think of the model as the interface to the whole application so you can reuse the model API - regardless of the controller/view styles (ie: JavaScript, Desktop, static HTML etc).

For instance at work we have a series of REST API which are used in extJS, Visual Basic and other web sites acting as a front-end interface.

A lot of frameworks have html code in their views. This is WRONG. In my opinion you should always have html in seperate template files and have your views parse these templates (fill them with variables from the models for example).

Agreed for the most part. A view is nothing more than an API abstraction of the interface - the template or output layer would then render accordingly - XHTML/HTML5/JS/GTK whatever.

Regards,
Alex

Yes. Business logic should go in the model for sure.

By “least responsibilities” I mean that models only care for themselves and handle their own thing. Controllers and Views have a “better understanding” about what goes on in the application and thus they carry more responsibility.

By “least responsibilities” I mean that models only care for themselves and handle their own thing

That’s just good OOP :slight_smile: .

Controllers and Views have a “better understanding” about what goes on in the application and thus they carry more responsibility.

I think I disagree with that. The Model (the Domain model, not the persistence model) is the core, the brain of the application, the purpose the application exists for. It reprezents the situations,problems and solutions for which the application is valuable for a stakeholder. The Controller is a coordinator, it routes relevant data to the relevant Model and selects the View which matches the outcome of the Model processing. The View has the least understanding as it show only a specific aspect of the Model.

I second the sugestion to take a look at Symfony. It’s a powerful MVC framework, it will be very useful for you to learn it and use it.