MVC 的目录结构

发布于 2024-12-13 03:45:58 字数 502 浏览 3 评论 0原文

我正在尝试清理我一直在研究的框架。现在,该站点由以下目录组成:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

朮生 2024-12-20 03:45:58

我建议您研究框架的目录结构,例如 symfony2 或 yii

这是我为我选择的:

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

您还可以使用文件命名约定,例如: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:

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.

酒几许 2024-12-20 03:45:58

我建议遵循 Symfony 1.x 目录结构。清晰、逻辑、安全。

摘自书籍“ Symfony 权威指南》 作者: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/ 包含项目的每个应用程序的一个目录(通常是前端
    以及前台和后台的后端)。
  • cache/ 包含配置的缓存版本,以及(如果激活它)
    项目的操作和模板的缓存版本。缓存机制
    (第 12 章中有详细介绍)使用这些文件来加速对 Web 请求的响应。
    每个应用程序都会在这里有一个子目录,包含预处理后的 PHP
    和 HTML 文件。
  • config/ 保存项目的常规配置。
  • data/ 这里,你可以存储项目的数据文件,比如数据库模式、SQL
    创建表的文件,甚至是 SQLite 数据库文件。
  • doc/ 存放项目文档,包括你自己的文档和
    PHPdoc 生成的文档。
  • lib/ 专用于外部类或库。这里可以添加需要的代码
    在您的应用程序之间共享。 model/ 子目录存储
    项目的对象模型(第 8 章中描述)。
  • log/ 存储 symfony 直接生成的适用日志文件。它还可以包含
    Web 服务器日志文件、数据库日志文件或项目任何部分的日志文件。
    Symfony 为每个应用程序和每个环境创建一个日志文件(日志文件是
    第 16 章讨论)。
  • plugins/ 存储应用程序中安装的插件(插件在章节中讨论)
    17)。
  • test/ 包含用 PHP 编写的单元和功能测试,并与
    symfony 测试框架(第 15 章讨论)。在项目设置期间,
    symfony 会自动添加一些存根和一些基本测试。
  • web/ Web 服务器的根目录。唯一可以从 Internet 访问的文件是
    位于该目录中的。

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.
前事休说 2024-12-20 03:45:58

我不会称自己为专家,但一种解决方案是将您的“框架”从实施中移开。我的意思是将您的“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 :)

一抹微笑 2024-12-20 03:45:58

我无法编辑 @kta 的答案,因为编辑队列已满,但该链接可能很快就会受到链接腐烂的影响。 Zend Framework 网站即将消失,并由 Laminas 项目取代。以下是原始链接的摘要:

下面描述了列出的每个目录的用例。

application/:此目录包含您的应用程序。它将容纳 MVC 系统以及配置、使用的服务和引导文件。

application/configs/:应用程序范围的配置目录。

application/controllers/、application/models/ 和 application/views/:这些目录用作默认控制器、模型或视图目录。将这三个目录放在应用程序目录中可以为启动简单项目以及启动具有全局控制器/模型/视图的模块化项目提供最佳布局。

application/controllers/helpers/:这些目录将包含操作助手。操作助手将被命名为默认模块的“Controller_Helper_”或其他模块中的“_Controller_Helper”。

application/layouts/:此布局目录用于基于 MVC 的布局。由于 Zend_Layout 能够支持基于 MVC 和非 MVC 的布局,因此该目录的位置反映了布局与控制器不是一对一的关系,并且独立于views/中的模板。

application/services/:此目录用于存放为您的应用程序提供的特定于应用程序的 Web 服务文件,或者用于为您的模型实现服务层。

application/Bootstrap.php:此文件是应用程序的入口点,并且应该实现 Zend_Application_Bootstrap_Bootstrapper。该文件的目的是引导应用程序并通过初始化组件来使组件可供应用程序使用。

modules/:模块允许开发人员将一组相关控制器分组为逻辑组织的组。模块目录下的结构类似于应用程序目录下的结构。

data/:此目录提供存储易失且可能临时的应用程序数据的位置。该目录中的数据受到干扰可能会导致应用程序失败。此外,此目录中的信息可能会或可能不会提交到颠覆存储库。此目录中的内容示例包括会话文件、缓存文件、sqlite 数据库、日志和索引。

docs/:此目录包含生成的或直接创作的文档。

library/:该目录用于存放应用程序所依赖的公共库,并且应该位于 PHP include_path 上。开发人员应将其应用程序的库代码放置在该目录下的唯一命名空间中,遵循 PHP 手册的 » 用户区命名指南 中制定的准则以及 Zend 本身制定的准则。该目录还可能包括 Zend Framework 本身;如果是这样,您可以将其放置在library/Zend/中。

public/:此目录包含您的应用程序的所有公共文件。 index.php 设置并调用 Zend_Application,后者又调用 application/Bootstrap.php 文件,从而调度前端控制器。 Web 服务器的 Web 根目录通常设置为此目录。

scripts/:此目录包含维护和/或构建脚本。此类脚本可能包括命令行、cron 或 phing 构建脚本,这些脚本不在运行时执行,但属于应用程序正确运行的一部分。

temp/:temp/ 文件夹是为临时应用程序数据预留的。此信息通常不会提交到应用程序 svn 存储库。如果 temp/ 目录下的数据被删除,应用程序应该能够继续运行,但性能可能会有所下降,直到数据再次恢复或重新缓存。

tests/:该目录包含应用程序测试。这些可以是手写的、PHPUnit 测试、基于 Selenium-RC 的测试或基于其他测试框架。默认情况下,可以通过模仿库/目录的目录结构来测试库代码。此外,可以模仿应用程序/目录结构(包括应用程序子目录)来编写应用程序的功能测试。

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:

The following describes the use cases for each directory as listed.

application/: This directory contains your application. It will house the MVC system, as well as configurations, services used, and your bootstrap file.

application/configs/: The application-wide configuration directory.

application/controllers/, application/models/, and application/views/: These directories serve as the default controller, model or view directories. Having these three directories inside the application directory provides the best layout for starting a simple project as well as starting a modular project that has global controllers/models/views.

application/controllers/helpers/: These directories will contain action helpers. Action helpers will be namespaced either as "Controller_Helper_" for the default module or "_Controller_Helper" in other modules.

application/layouts/: This layout directory is for MVC-based layouts. Since Zend_Layout is capable of MVC- and non-MVC-based layouts, the location of this directory reflects that layouts are not on a 1-to-1 relationship with controllers and are independent of templates within views/.

application/services/: This directory is for your application specific web-service files that are provided for your application, or for implementing a Service Layer for your models.

application/Bootstrap.php: This file is the entry point for your application, and should implement Zend_Application_Bootstrap_Bootstrapper. The purpose for this file is to bootstrap the application and make components available to the application by initializing them.

modules/: Modules allow a developer to group a set of related controllers into a logically organized group. The structure under the modules directory would resemble the structure under the application directory.

data/: This directory provides a place to store application data that is volatile and possibly temporary. The disturbance of data in this directory might cause the application to fail. Also, the information in this directory may or may not be committed to a subversion repository. Examples of things in this directory are session files, cache files, sqlite databases, logs and indexes.

docs/: This directory contains documentation, either generated or directly authored.

library/: This directory is for common libraries on which the application depends, and should be on the PHP include_path. Developers should place their application's library code under this directory in a unique namespace, following the guidelines established in the PHP manual's » Userland Naming Guide, as well as those established by Zend itself. This directory may also include Zend Framework itself; if so, you would house it in library/Zend/.

public/: This directory contains all public files for your application. index.php sets up and invokes Zend_Application, which in turn invokes the application/Bootstrap.php file, resulting in dispatching the front controller. The web root of your web server would typically be set to this directory.

scripts/: This directory contains maintenance and/or build scripts. Such scripts might include command line, cron, or phing build scripts that are not executed at runtime but are part of the correct functioning of the application.

temp/: The temp/ folder is set aside for transient application data. This information would not typically be committed to the applications svn repository. If data under the temp/ directory were deleted, the application should be able to continue running with a possible decrease in performance until data is once again restored or recached.

tests/: This directory contains application tests. These could be hand-written, PHPUnit tests, Selenium-RC based tests or based on some other testing framework. By default, library code can be tested by mimicing the directory structure of your library/ directory. Additionally, functional tests for your application could be written mimicing the application/ directory structure (including the application subdirectory).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文