# How to structure a (true) MVC application?

I am working on a PHP project (the code should also be readable by non PHP people though) which I am trying to setup using a true MVC structure. Or at least as closed as I can. What I am currently doing is the following:

# bootstrap file

$request = new \MyApp\Services\Http\Request();$storage    = new \MyApp\Services\Storage\Session();
$token = new \MyApp\Services\Token($storage);
$user = new \MyApp\Services\User($storage);
$menu = new \MyApp\Services\Menu($token, $user);$view       = new \MyApp\Views\Index($request,$menu);
$controller = new \MyApp\Controllers\Index();$response   = $controller->frontpage($view);

{
return $view->render(); } }  And in my view I use the request object and the menu object to send the correct menuitems to the template(s). The request object is used to give the currently selected menuitem (of the current page) another class in the template. And I have a render() method which simply returns the rendered HTML. However AFAICT the above is flawed (in the sense that it hasn't much to do with an MVC structure), because: • the controller uses a view instance • because of this the controller now is somewhat responsible for presentation • the view uses the request object (which should have been the controllers task) So what I was thinking of doing was the following: # bootstrap file $request    = new \MyApp\Services\Http\Request();
$storage = new \MyApp\Services\Storage\Session();$token      = new \MyApp\Services\Token($storage);$user       = new \MyApp\Services\User($storage);$menu       = new \MyApp\Services\Menu($token,$user);
$controller = new \MyApp\Controllers\Index($request);
$controller->frontpage($menu);
$view = new \MyApp\Views\Index($menu);

echo $view->render();  # Index controller namespace MyApp\Controllers; class Index { private$request;

public function __construct($request) {$this->request = $request; } public function frontpage($menu)
{
$menu->setRequest($request);
}
}


Now the controller takes care of the request and updates the menu part of the model layer and the only thing the view has to do now is simply retrieve all the "active" menu items from the menu service.

My second code sample makes more sense to me (when talking about a true MVC structure). Is my assumption correct? Or are both case off? Anything else?

-
I see that your making the attepmt to use MVC along with PHP. I suggest looking into Yii. It is a PHP framwork that utilizes MVC. You can see how folder are structured right down to the code. I hope this helps. Check it out: yiiframework.com –  user1880670 Mar 28 '13 at 21:29
Overall this doesn't look bad, but one thing in particular sticks out in my eyes: the menu. What is it supposed to model? Why does it have such a prominent role? Isn't it just a part of the view? –  Jon Mar 28 '13 at 23:15
@user1880670 I have looked at Yii and not only is the thing filled with statics everywhere it just advocates terrible practices all over the code base. –  user6669 May 9 '13 at 17:37
Yii is one of the three worst popular frameworks for PHP. –  PeeHaa Dec 10 '13 at 11:36
Where does \$request = new \MyApp\Services\Http\Request(); get its request data from? –  PeeHaa Dec 10 '13 at 11:37