views:

230

answers:

3

I'm relatively well versed in designing websites. I mostly go for LAMP where I already have a small "framework" of my own, that I use. In short, it separates the logic from the layout and I have basically one logic file to one or many layout files depending on what views are supported in the layout. There's an admin section and there are user auth and all such stuff. Fine.

So, when looking to get more elaborate in my way of designing / programming in PHP - or website programming at large - I'm wondering how to "think" MVC properly. I'm leaning towards this way since my current framework is very DB oriented and has become somewhat heavy where performance is of the essence.

So here's my question: Am I correct in assuming that a controller typically corresponds to a "section" or "page" and a view takes care of displaying that controller, and that a model handles the objects used by the controller and being displayed in the views?

Let's take an example (not too elaborate, but enough to see if my thinking is valid):

Say we have a simple games website. The sections would typically be something like: frontpage, games, forums, and about / disclaimers etc.

The controller classes corresponds to the sections but gets a little more elaborate to cover the "single instance" version of the object covered by the section i.e. games section becomes two controllers; one for games overview (list of games) and one for the game page itself. The whole would be something like frontpage, games-overview, gamepage, forums, forum, topic-page, about, and disclaimer.

The views can be a number of layouts for each controller e.g. the same as the controllers but perhaps various types of views on the forums pages (depending on how you want to view them) and games pages (maybe a highscore view) and so on.

The model (or dataobjects) are typically users, games, forums, topics, posts, and then a lot of helper objects like topic tags, game highscores and what not to make the topics possible to categorize and the games able to have highscores etc etc.

Is the above "correct" way of thinking or am I totally misunderstanding the whole M-V-C concept for websites.

I'm thinking about moving over to CodeIgniter or some other light framework (feel free to comment on framework choice or if it is better to go for my own) since my own framework is very DB-oriented and is not cutting it now that a couple of my sites are surpassing 70,000 pageviews / day.

A sincere thank you to those of you who can help in answer whether or not my take on MVC is somewhat correct and also if possible, add a few hints of what to think about when coding MVC and still want to maintain cutting edge performance (as much that is possible with scripted languages).

A: 

Wikipedia is probably better at explaining this, but basically,

your model represents, and abstracts, your underlying data (e.g. provides an abstraction layer so the other elements don't have to interact with the database... or even know that it is there).

your controller handles requests to change the model.

your view is the display of part of the model and feedback from the controller.

Neither the controller nor the model should handle display. display. Neither the model nor the view should handle incoming POST data or the like. Neither the controller nor the view should deal with the data sources.

see http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller#Description for a good description of the roles.

What you describe in the question does not fit this description. There should be, say, one controller per model class (or less) to handle requests to the elements those model classes represent.

Your views should not be explicitly tied to any one controller or model element. the view represents a subset of the model as a whole.

The only interaction between a view and a controller is the display of feedback.

Hopefully that helps.

EDIT: As other have pointed out there are a number of MVC frameworks already built. (See http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller#PHP for a list) and I'd advise against being too attached to a framework you've already built if there's another that does the job better.

Jonathan Fingland
A: 

CodeIgniter is a great tool for MVC beginners, I would suggest you look at net.tutsplus.com there is an ongoing video tutorial on CodeIgniter

added

for beginners wanting to dive into CI, links to videotuts:

added

I don't recommend CI as great and powerful framework, but it's simple for beginners and they can learn a lot from it, personally i'm in favor of Symfony and Yii

Juraj Blahunka
I don't recommend CI, it is a very restricting framework which lays chains on your hands for advanced usage and it is oriented towards PHP 4 which limits it strongly.
tharkun
I know, but it simplifies the structure for a beginner in MVC frameworks. After such simplistic and limited fw, a programmer can move to more advanced stuff like Symfony or Yii
Juraj Blahunka
Agree, that's a good point. I just wouldn't recommend anyone to invest time into CI if they won't use it later on. Reading Symfony or Zend Framework will do the same job of explaining the MVC structure plus the time invested might pay off because you will actually use the frameworks.
tharkun
You're right, but as you said, grasping mvc pattern is not only about web apps. so learning it everyvhere else (smalltalk, custom implementations) and then applying it to web frameworks. I even think, that the best way to get in touch with mvc frameworks is to study their source code and then try to develop better framework yourself..
Juraj Blahunka
+1  A: 

First of all, this is not a php specific question but rather a question about how to apply the MVC pattern to web programming. And indeed the MVC pattern makes so much sense in the area of web application development that it has almost become an imperative in this field of application.

Now, as for the MVC pattern itself, you will find very good answers on SO already. For example the Question and Answers of What goes into the controller.

As for the question of how to map your website structure to your controller architecture I would recommend the following.

  • don't try to map your navigation but rather the application logics
  • one controller for each logical unit of functions
  • inside the controllers, one public method for each view
  • rather lean controllers which mainly play gateway between models and views

So for example if in your case gameoverview is just a list, it doesn't need its own controller but rather is just a method in the indexcontroller that gets the list from the model and sends it to the respective view. But if your gameoverview is a complex mechanism with many possibilities and different subviews, etc. you could have an overviewController with several methods for the different views and tasks.

I really recommend looking into Zend Framework and how these problems are solved there. IMO you can learn really a lot about the topic of your question by doing that.

tharkun