Search code examples
model-view-controllerarchitecturephp

Directory Structure for MVC


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.


Solution

  • 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.