Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MVC - How to design?

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).

like image 916
Adergaard Avatar asked Oct 15 '22 13:10

Adergaard


2 Answers

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.

like image 181
markus Avatar answered Nov 03 '22 03:11

markus


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:

  • CI day 1
  • CI day 2
  • CI day 3 - sending emails
  • CI day 4 - newsletter signup
  • CI day 5 - basic CRUD
  • CI day 6 - login
  • CI day 7 - pagination

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

like image 41
Juraj Blahunka Avatar answered Nov 03 '22 03:11

Juraj Blahunka