In the framework I use (Zend Framework) MVC works like this. Controllers & Models are classes. Views are just scripts. A view might just be some HTML and the only PHP will be very simple, usually just echo'ing a variable.
It actually all comes together like this
1) user requests a URL through their browser /user/view/1
2) A "route" (usually just a string or an array that "configures" the framework) is added to the "router" by the programmer (the router is a global object). The route is setup with a string usually, like this
:controller/:action/:id
like a pattern match for the above request URL in #1
3) the "Front controller" (your index.php basically) goes through all the routes and finds the one that matches the request. This is all done by the framework automatically. All you actually do is setup the route and start coding. So it would load the User controller which is a class called UserController. It would call the view action since that's what was in the request URL. So the "front controller" looks at the request, the routes, and finds the "controller" (called UserController) and calls the viewAction() method
4) This viewAction() method internally would find the user:
Code: Select all
function viewAction() {
$user = $this->findUser();
....
.. and then it assigns it to the "view object" (which is different than the view script) ...
The framework uses the name of the controller & action from the request to find the view script (user/views/view.phtml) so (:controller:/views/:action:.phtml).. something like that. It does it for me based on the name of the function & class of the controller & action.
5) Execution in the controller has now been passed off to the "view script". THis is what programmers talk about when they refer to a "stack". Each time execution is passed from one file to another, like an include()
Code: Select all
<div class="hello world">Hello <?=$this->user->name()?></div>
So to recap. A request basically makes the controller execute, which in turn grabs a model and "pushes" it to the view.
I have described one type of MVC. "model push".
Here's the guy I learned from, he posts here and works on the Zend Framework:
http://blog.astrumfutura.com/archives/3 ... nning.html
To take it to the next level you could have different views for any single given action. So you could let someone view something in plain text, or a variety of different HTML layouts. Like when you are shopping and they have "grid view", "list view", etc... all for the same page, you wonder how they do it? MVC. Heck a "view" could be just a program that beeps the output to the user in morse code.