MVC Tutorial

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
blueyon
Forum Commoner
Posts: 76
Joined: Tue Oct 30, 2007 9:53 am

MVC Tutorial

Post by blueyon »

Following my request on this thread viewtopic.php?f=19&t=81192 I thought the members of the forum could create a group MVC tutorial.

To start we should build a list of possible classes for the tutorial.

Front Controller
Page Controller
Action class / Transfer Object
Router
Request
Response
Registry

Please add more if you think they should be included.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MVC Tutorial

Post by Christopher »

I think perhaps leave the Page Controller for later. You can usually just initialize the Action Controller to achieve the same thing. You might want to add View and PHP Template classes.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: MVC Tutorial

Post by alex.barylski »

I didnt' read the other thread...there is some serious MVC overload going on here. :P

If you are just starting out, I suggest implementing a simple front controller via a switch statement:

http://www.phppatterns.com/docs/design/ ... er_and_php

It nails home the point behind the centralization of a front controller. From there you can work on making it more dynamic by loading the said 'action' controllers.

I think it's best to demonstrate how MVC works by first starting with a monolithic example and slowly refactoring each section into it's MVC component.

For example: First remove the HTML from a script into a template and then move the view code into a view class and call the template. Then start with the model, then worry about the controllers, as they are always most interesting; Save the best for last.

Cheers :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MVC Tutorial

Post by Christopher »

Hockey wrote:I didnt' read the other thread...there is some serious MVC overload going on here. :P

If you are just starting out, I suggest implementing a simple front controller via a switch statement:

http://www.phppatterns.com/docs/design/ ... er_and_php

It nails home the point behind the centralization of a front controller. From there you can work on making it more dynamic by loading the said 'action' controllers.
I remember that article and I am not sure why anyone would recommend it. That article expresses the long discredited opinion that the web server is the Front Controller and implementing the Front Controller pattern in PHP is wrong. The reason for that opinion was that the writer and many others at the time did not really understand the Front Controller pattern. It is evident from the article that the writer can't shake the Page Controller pattern to be able to see the Front Controller pattern.
Hockey wrote:I think it's best to demonstrate how MVC works by first starting with a monolithic example and slowly refactoring each section into it's MVC component.
How do you start monolithic when you have no parts? Where are the lithos? The switch example in the article you reference is a Page Controller. That will start things on the wrong foot. I suggest starting with simple versions of the objects around the Front Controller first, because those are the things that the programmer touches -- the Request, Response, Action Controller. Once those are useful, then create a single entry point that uses and runs those objects.
Hockey wrote:For example: First remove the HTML from a script into a template and then move the view code into a view class and call the template. Then start with the model, then worry about the controllers, as they are always most interesting; Save the best for last.
We currently have not HTML to remove, so that should be easy! ;) I',m still not sure the difference between view and templates. I have developed a sneaking suspicion that template engines are view classes. The Models are in a different layer so we alway know they are independent and therefore cannot really relate to this design process.
(#10850)
webaddict
Forum Commoner
Posts: 60
Joined: Wed Mar 14, 2007 6:55 am
Location: The Netherlands

Re: MVC Tutorial

Post by webaddict »

Hockey wrote:For example: First remove the HTML from a script into a template and then move the view code into a view class and call the template. Then start with the model, then worry about the controllers, as they are always most interesting; Save the best for last.
Actually, I find the Model layer more interesting by far. It could be that's because my controller layer isn't that sophisticated.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MVC Tutorial

Post by Christopher »

webaddict wrote:Actually, I find the Model layer more interesting by far. It could be that's because my controller layer isn't that sophisticated.
I agree. I have been thinking about creating classes for Skeleton that encourage creating a Domain Model that contains the Model classes (and aggregrates). Something that leads to creating a Domain Model due to it's design.

One problem with MVC implementations is that they are Controller focussed because programmers are essentially procedural in mindset no matter what you do. MVC implementations also tend to scatter the Models in many directories. It helps organize things (more helpful for Controllers and Views really), but you lose the sense of the relationships between the Models that should exist to create a Domain Mode. You really should start in MVC building Models, then Views, and code the Controllers last. But you would need some sort of test harness to do that because need to run the Model and View classes in a Controller-like environment.

webaddict - if you are interested in the Model layer, I could really use your design help on Skeleton.
(#10850)
blueyon
Forum Commoner
Posts: 76
Joined: Tue Oct 30, 2007 9:53 am

Re: MVC Tutorial

Post by blueyon »

arborint,

When you say domain model do you mean "Domain-specific programming language"?

How would you set models up?

$model->get('product')->update($_POST);
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MVC Tutorial

Post by Christopher »

blueyon wrote:When you say domain model do you mean "Domain-specific programming language"?
No, by Domain Model I mean all the interdependent Model objects that "model" the domain for the application. For example, the domain for an ecommerce site might include Orders, Products, Categories, Cart, Checkout, Shipping, Tax, etc. All of those objects "model" concepts in retail. They do also form a language with which programmers and their clients can talk about the application -- because they are familiar terms to everyone.
blueyon wrote:How would you set models up?

$model->get('product')->update($_POST);
I don't think there is one solution to deal with all Data Sources or Services. You show an OO SQL style interface (Skeleton has one that Mordred raked over the coals to improve. See: viewtopic.php?f=19&t=80191). But there are other patterns, obviously the big ones are TransactionScript, ActiveRecord, RowDataGateway, TableDataGateway, OR Mapper, etc. The probelml I have been thinking about solving is how the Model objects interrelate and how to make it natural to build them as a coherent, interrelated whole.
(#10850)
Post Reply