Stupidly Easy MVC - Directory Structure

There are probably many ways to setup a structure using MVC and this article will talk about two of the ways I have done it and explain why I did it that way and how I think it works out after the fact.

Structure 1
For this project, I used Ruby On Rails as inspiration (Imitation is the best form of flattery, yes?) and used a separate directory for each of the Model, View and Controller. I knew I I was going to have multiple models as well. I used inline php/html for the view for this project since it was only a few forms and we didn't want to do a template system yet. It would be easy to change to templates later of course by just editing the view methods to call a template instead of spitting out html.

   |--- management
     |--- index.php
     |--- mvc_lib
     |--- model
        |--- User_Model.php
        |--- Permission_Model.php
        |--- User_Management_Model.php
     |--- view
        |--- User_Management_View.php
     |--- controller
         |--- User_Management_Controller.php
     |--- tests
  • In RoR the model,view, controller directors would NOT be in the htdocs directory. They would actually be plural as well.
  • index.php is where I call the User_Management_Controller class.
  • The User_Management_Model has methods that create a User_Model which adds, edits deletes. The User_Model initiates an instance of Permission_Model. I never use User_Model and Permission_Model in the User_Management_Model. Perhaps in RoR, these methods should go in the helper for the Controller (I'm in the process of learning RoR, so if anybody has any insight, speak up!).
  • The stylesheet I did inline in the method _showHeader() method of the User_Mangement_View. Just because it was just a simple stylesheet only a few lines. Otherwise I would of put a directory "style" in htdocs and put the stylesheet there and then just included it in my _showHeader() with a link tag (or @import, whatever you like).
  • For this application (later then my first one) I was a good kid and wrote tests as I went along. I used Chris Shifflet's test-more.php as well as Selenium. For more information on test-more see my blog post and Mr. Shifflet's response. I wrote an article about Selenium not too long ago (also check out Selenium IDE).

Let me tell you, it was very nice to have tests written. For example, after I had the User model built and working, I decided to store the password encrypted. I changed my User model, ran the tests, fixed bugs. When the tests passed I was sure that my changes didn't break existing functionality. Having Selenium was super nice and fast, because it sure gets boring typing in form fields to test an app while you are coding. Rock On.

This works well for a simple application. I would have rather put the model, view, controller in a directory that was not in htdocs (I didn't know the structure of where this site was going) but those can always be moved and the paths updated.

Structure 2
My very first application with my MVC classes was for an already existing, massively huge website. This site also uses Smarty. So I couldn't exactly go about breaking everything into model, view, controller directories. Here's how I laid it out:

      |--- List_Controller.php
      |--- List_Model.php
      |--- List_View.php
   |--- smarty
      |--- templates
         |--- list.css
         |-- index.php
  • lib/mvc contains the base MVC classes
  • list_app contains the files for my list application
  • htdocs/list/index.php is where I call the controller.

I think this works out really well if you want to try this with an existing site structure. It's a good idea to keep your files with sql code, database user/pass off of the htdocs path. This will prevent users from poking around and running files they shouldn't be putting in the URL.

This is just a few of the ways I've done it. I'm still in the process of forming these concepts into best practices. If you know of a better way, let us know!

Later this week - more examples!