MVC 的目录结构
我正在尝试清理我一直在研究的框架。现在,该站点由以下目录组成:
Models
Views
Controllers
Helpers (Miscellaneous functions)
Libraries (Universal classes, like library and session management)
Images
Style
每当调用页面时,路由器脚本都会查找关联的控制器,因此 thesite.com/login 会在“/controllers/login.php”处实例化 Login_Controller 我的问题是面临的是,路由器脚本本身就像一种控制器,就像 view.php 一样,它处理要由适当视图处理的格式化数据。但它们不太像页面控制器,因为它们控制 MVC 本身。我对这个架构还是有点陌生,我很好奇有更多经验的人会如何组织这个。
我可以将路由器和视图控制器分类为库,还是最好在 /controllers 内创建一个名为“pages”的子目录,或者任何其他想法?非常感谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议您研究框架的目录结构,例如 symfony2 或 yii
这是我为我选择的:
您还可以使用文件命名约定,例如:YourClassName.class.php 用于类,YourView.phtml 用于您的视图,等等。检查一个框架,您将学习如何良好地构建和应用程序。
I would suggest you to study a framework's directory structure, such as symfony2 or yii
here is what i chose for mine:
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.
我建议遵循 Symfony 1.x 目录结构。清晰、逻辑、安全。
摘自书籍“ Symfony 权威指南》 作者:Fabien Potencier & François Zaninotto :
以及前台和后台的后端)。
项目的操作和模板的缓存版本。缓存机制
(第 12 章中有详细介绍)使用这些文件来加速对 Web 请求的响应。
每个应用程序都会在这里有一个子目录,包含预处理后的 PHP
和 HTML 文件。
创建表的文件,甚至是 SQLite 数据库文件。
PHPdoc 生成的文档。
在您的应用程序之间共享。 model/ 子目录存储
项目的对象模型(第 8 章中描述)。
Web 服务器日志文件、数据库日志文件或项目任何部分的日志文件。
Symfony 为每个应用程序和每个环境创建一个日志文件(日志文件是
第 16 章讨论)。
17)。
symfony 测试框架(第 15 章讨论)。在项目设置期间,
symfony 会自动添加一些存根和一些基本测试。
位于该目录中的。
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 :
and backend for the front and back office).
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.
file that creates tables, or even a SQLite database file.
documentation generated by PHPdoc.
to be shared among your applications. The model/ subdirectory stores the
object model of the project (described in Chapter 8).
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).
17).
symfony testing framework (discussed in Chapter 15). During the project setup,
symfony automatically adds some stubs with a few basic tests.
ones located in this directory.
我不会称自己为专家,但一种解决方案是将您的“框架”从实施中移开。我的意思是将您的“router”、“view.php”和其他框架类移动到某个外部位置,然后将其包含在您的index.php 或任何将成为您的访问点的文件中。
那么只有内容会位于您的实际应用程序目录中,而所有框架文件将位于无法通过 Web 服务器访问的位置。
只是一个想法:)
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 :)
我真的很喜欢Zend 推荐的项目目录结构。
I really like Zend's Recommended Project Directory Structure.
我无法编辑 @kta 的答案,因为编辑队列已满,但该链接可能很快就会受到链接腐烂的影响。 Zend Framework 网站即将消失,并由 Laminas 项目取代。以下是原始链接的摘要:
I could not edit @kta's answer because the edit queue is full, but that link may suffer from link rot soon. The Zend Framework website is going away and being replaced by the Laminas Project. Here is a summary of the original link: