php MVC 的目录结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7959673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Directory Structure for MVC
提问by dlwiest
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.
任何时候调用页面时,路由器脚本都会查找关联的控制器,因此 thesite.com/login 会在 '/controllers/login.php' 实例化 Login_Controller 我面临的问题是,路由器脚本本身感觉像是一种类型控制器,view.php 也是如此,它处理由适当视图处理的格式化数据。但是这些不太像页面控制器,因为它们控制 MVC 本身。我对这种架构还是有点陌生,我很好奇有更多经验的人会如何组织它。
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.
我可以将路由器和视图控制器归类为库,还是在 /controllers 中创建一个名为“pages”的子目录或任何其他想法会更好吗?非常感谢。
采纳答案by Frosty Z
I would suggest to follow the Symfony 1.x directory structure. Clear, logical, secure.
我建议遵循Symfony 1.x 目录结构。清晰、合乎逻辑、安全。
Excerpt from book "The definitive guide to Symfony"by Fabien Potencier & Fran?ois Zaninotto :
Fabien Potencier 和 Fran?ois Zaninotto所著的“Symfony权威指南”一书摘录:
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.
- apps/为项目的每个应用程序包含一个目录(通常为前台和后台的前端和后端)。
- cache/包含配置的缓存版本,以及(如果您激活它)项目操作和模板的缓存版本。缓存机制(详见第 12 章)使用这些文件来加速响应 Web 请求。每个应用程序在此处都有一个子目录,其中包含预处理的 PHP 和 HTML 文件。
- config/保存项目的一般配置。
- data/在这里,您可以存储项目的数据文件,例如数据库模式、创建表的 SQL 文件,甚至是 SQLite 数据库文件。
- doc/存放项目文档,包括你自己的文档和PHPdoc生成的文档。
- lib/专用于外部类或库。在这里,您可以添加需要在您的应用程序之间共享的代码。该模型/子目录存储对象项目的模型(在第8章所描述的)。
- log/存储 symfony 直接生成的适用日志文件。它还可以包含来自项目任何部分的 Web 服务器日志文件、数据库日志文件或日志文件。Symfony 为每个应用程序和每个环境创建一个日志文件(日志文件在第 16 章中讨论)。
- plugins/存储应用程序中安装的插件(插件在第 17 章中讨论)。
- test/包含用 PHP 编写的单元和功能测试,并与 symfony 测试框架兼容(在第 15 章中讨论)。在项目设置期间,symfony 会自动添加一些带有一些基本测试的存根。
- web/Web 服务器的根目录。可从 Internet 访问的唯一文件是位于此目录中的文件。
回答by Packet Tracer
I would suggest you to study a framework's directory structure, such as symfony2 or yii
我建议你研究一个框架的目录结构,比如 symfony2 或 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.
您还可以使用文件命名约定,例如:类的 YourClassName.class.php,视图的 YourView.phtml 等。检查框架,您将学习如何很好地构建和应用程序。
回答by RandomWhiteTrash
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.
我不会称自己为专家,但一种解决方案是将您的“框架”从实施中移开。我的意思是将您的“路由器”、“view.php”和其他框架类移动到某个外部位置,然后将其包含在您的 index.php 或任何文件中作为您的访问点。
Then only content would be in your actual application directory while all framework files would be in a location not accessible via web server.
那么只有内容将位于您的实际应用程序目录中,而所有框架文件将位于无法通过 Web 服务器访问的位置。
Just an idea :)
只是一个想法:)
回答by kta
I really like Zend's Recommended Project Directory Structure.
我真的很喜欢Zend 的推荐项目目录结构。