Directory Structure for MVC

38,175

Solution 1

I would suggest to follow the Symfony 1.x directory structure. Clear, logical, secure.

Excerpt from book "The definitive guide to Symfony" by Fabien Potencier & François Zaninotto :

apps/
  frontend/
  backend/
cache/
config/
data/
  sql/
doc/
lib/
  model/
log/
plugins/
test/
  bootstrap/
  unit/
  functional/
web/
  css/
  images/
  js/
  uploads/
  • apps/ Contains one directory for each application of the project (typically, frontend and backend for the front and back office).
  • cache/ Contains the cached version of the configuration, and (if you activate it) the cache version of the actions and templates of the project. The cache mechanism (detailed in Chapter 12) uses these files to speed up the answer to web requests. Each application will have a subdirectory here, containing preprocessed PHP and HTML files.
  • config/ Holds the general configuration of the project.
  • data/ Here, you can store the data files of the project, like a database schema, a SQL file that creates tables, or even a SQLite database file.
  • doc/ Stores the project documentation, including your own documents and the documentation generated by PHPdoc.
  • lib/ Dedicated to foreign classes or libraries. Here, you can add the code that needs to be shared among your applications. The model/ subdirectory stores the object model of the project (described in Chapter 8).
  • log/ Stores the applicable log files generated directly by symfony. It can also contain web server log files, database log files, or log files from any part of the project. Symfony creates one log file per application and per environment (log files are discussed in Chapter 16).
  • plugins/ Stores the plug-ins installed in the application (plug-ins are discussed in Chapter 17).
  • test/ Contains unit and functional tests written in PHP and compatible with the symfony testing framework (discussed in Chapter 15). During the project setup, symfony automatically adds some stubs with a few basic tests.
  • web/ The root for the web server. The only files accessible from the Internet are the ones located in this directory.

Solution 2

I would suggest you to study a framework's directory structure, such as symfony2 or yii

here is what i chose for mine:

public_html/              (for public files) (should be public, place only index.php in here)
public_html/css/
public_html/images
public_html/js            (for your js files, or custom generated ones)
lib/                      (for my libs)  (should be private)
lib/vendor/               (for 3rd party libs)
application/              (for the whole app) (should be private)
application/class         (classes that make the app work such as mainApp, Controller, Model, View, etc...)
application/class/model   (all the models)
application/class/view    (all the views)
application/class/view/html (templates used by the views)
application/class/controller (all controllers)
application/class/helper  (helper functions)
application/class/lib     (libs that you develop for the application)
application/template      (layout and/or templates for the application)
application/conf          (config files)
application/log           (log files)
application/cron          (scheduled jobs)
application/database      (for database migration scripts)
...

You can also use file naming conventions, such as: YourClassName.class.php for clases, YourView.phtml for your views, etc. Check a framework and you'll learn how to structure nicely and app.

Solution 3

I would not call myself an expert but one solution would be to move your 'framework' away from implementation. What I mean is to move your 'router', 'view.php' and other framework classes to some external location which you then include in your index.php or whatever file would be your access point.

Then only content would be in your actual application directory while all framework files would be in a location not accessible via web server.

Just an idea :)

Share:
38,175

Related videos on Youtube

dlwiest
Author by

dlwiest

Updated on July 09, 2022

Comments

  • dlwiest
    dlwiest almost 2 years

    I'm trying to clean up the framework I've been working on. Right now, the site consists of the following directories:

    Models
    Views
    Controllers
    Helpers (Miscellaneous functions)
    Libraries (Universal classes, like library and session management)
    Images
    Style
    

    Any time a page is called, the router script looks up the associated controller, so thesite.com/login would instantiate Login_Controller at '/controllers/login.php' The problem I'm facing is, the router script itself feels like a type of controller, as does view.php, which handles formatting data to be handled by the appropriate view. But these aren't quite like page controllers, since they control the MVC itself. I'm still somewhat new to this architecture, and I'm curious how someone with more experience would organize this.

    Could I classify the router and view controllers as libraries, or would it be better to create a subdirectory inside /controllers called 'pages', or any other ideas? Thanks so much.

    • Brian Glaz
      Brian Glaz over 12 years
      Typically the folders you listed above would be in one folder called 'app' for example, and the code actually running your framework would be stored in it's own folder called 'core' for example.
    • dqhendricks
      dqhendricks over 12 years
      You will also probably want to move most of this out of your public directory, and use an include path to grab files. I am assuming it is in a public directory currently because of the Style and Images folder.
    • dlwiest
      dlwiest over 12 years
      So I would have model, view, controller directories on the site root, and then /application/controller for router.php and view.php? Just wondering what's considered standard, if anything.
  • dlwiest
    dlwiest over 12 years
    Thank you, that's very helpful. I'm assuming view.php would go in /view, and then all the rest of the template files would go in /view/html, but where would you recommend putting router.php? It's pretty much the entry point to the rest of the system.
  • Packet Tracer
    Packet Tracer over 12 years
    for the router I use /index.php, it's the bootstrapper, inside it I use an instance off mainApp class, that manages any "global operations" (routing, caching, controller invocation, logging, errors)... though I do the routing stuff via apache's mod_rewrite module
  • Packet Tracer
    Packet Tracer over 12 years
    about the view...you're but i'm editing my answer so it shows more clear what i intend to explain
  • geilt
    geilt over 11 years
    I used a ton from the map above, it was really helpful. I especially loved the bit about moving all the folders above the public folder for security. Top notch! In terms of a router, I created a route.php in the base application directory of my system. It sits there with the config.php. I use config to establish file directories and other important info, and the route just includes config and runs. So my app only has to call the following in index.php require('../app/router.php'); //Initialize Router $app = new Router; //Route and Run $app->route(); And off it goes!
  • Mike
    Mike about 7 years
    The link's dead.
  • Frosty Z
    Frosty Z about 7 years
    Thanks for the info Mike ; updated my answer + copy/pasted/formatted relevant excerpt from the original doc
  • Esteban
    Esteban over 2 years
    @FrostyZ Nowadays Symfony is version 5.x Do you have the same link but for the latest version of Symfony? version 1.x is outdated
  • Frosty Z
    Frosty Z over 2 years
    @Esteban Here is the directory structure from Symfony 6.0 doc symfony.com/doc/current/… but looks not as well documented as for 1.x. I'm not using Symfony anymore so I won't be able to explain in detail. Maybe I should delete my answer now looking obsolete.
  • Esteban
    Esteban over 2 years
    @FrostyZ (1/2) What framework are you using now? I was reading Laminas directory structure and I see it a little bit complex for answering the original question. I see in almost all frameworks that for each application or "web" module it has Model, Controller & View folders in order to achieve MVC arquitecture, but how about if I have 3 applications (in the same project as they reuse the same business logic...) how could be the directory structure in that case?
  • Esteban
    Esteban over 2 years
    (2/2) I mean: I have the business/domain application logic, mapper and entity classes inside the Model folder. I have 3 frontends inside my project, one is the public site, other one is kind of an intranet and the last one is an administration site. I havent separated them in 3 different projects.. as they consume the same business logic and I want to reuse the Model layer. How would be the directory structure,.. as I have seen in all frameworks that the Model folder/layer is just for only one app/module? Can I break that rule?
  • Frosty Z
    Frosty Z over 2 years
    I'm using JS frameworks now (Vue, Angular...). However on PHP I've been interested by Laravel. Here is an article that should help you to solve your problem of "reusing a model across serveral applications" with that framework ambujsoni.medium.com/…
  • Blizzardengle
    Blizzardengle about 2 years
    Hi kta, I know this is a old answer, but your link may suffer from link rot soon. Would you mind revisiting this answer?