MVC is not a file structure

Recently I’ve been seeing new projects laid out with a file structure attempting to dictate an MVC architecture, with model, view and controller folders. For many web applications though, this is much too limiting. It’s a structure that suggests that elements can be reused – could any element in the model folder use any element in the view folder? I suspect not. Implementations I’ve seen attempt the following structure (usually under model and view folders):
/model/
home.php
login.php
users.php
/view
home.php
login.php
users.php

Shared elements are suited to this structure, for example classes that output standard pages, or standard model functions, but I prefer file structures that allow for use cases per file. Generally I create each as a separate class, but I’d argue that a file as simple as this can be considered as MVC architecture.

function load($mode){
/////////////////////
// CONTROLLER
switch($mode){
case “printHash”: printMenu($_POST['toHash']); break;
}
}

function printHash($toHash){
/////////////////////
// MODEL
$toHash = stripslashes($toHash);
$hash = sha1($toHash);

/////////////////////
// VIEW
echo $toHash.”=”.$hash;
}

Whilst at University I was taught too much about what MVC is rather than why and when it should be considered, and I think that other developers are suffering from the same tuition. The aim of an MVC architecture is to encourage independent development and testing and to present clean and logical code that is easy to maintain. If you can use MVC to achieve this then it does not matter how you structure your files.

Comments are closed.