Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 645
dlwiest Avatar asked Oct 31 '11 20:10

dlwiest


People also ask

What is folder structure in MVC?

Controllers Folder This folder will contain all the Controller classes. MVC requires the name of all the controller files to end with Controller. In our example, the Controllers folder contains two class files: AccountController and HomeController.

What is directory structure in asp net?

Below is a typical folder structure for an ASP.NET web pages web site: The "Account" folder contains logon and security files. The "App_Data" folder contains databases and data files. The "Images" folder contains images. The "Scripts" folder contains browser scripts.

What is App_Start folder MVC?

The App_Start folder of MVC application is used to contain the class files which are needed to be executed at the time the application starts. The classes like BundleConfig, FilterConfig, IdentityConfig, RouteConfig, and Startup. Auth etc. are stored within this folder.

How many layers are there in MVC?

It provides three main layers; model, view, and controller. Many developers use MVC as a standard design pattern.


3 Answers

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.

like image 172
Packet Tracer Avatar answered Sep 22 '22 05:09

Packet Tracer


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.
like image 20
Frosty Z Avatar answered Sep 21 '22 05:09

Frosty Z


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 :)

like image 28
RandomWhiteTrash Avatar answered Sep 20 '22 05:09

RandomWhiteTrash