PHP 项目的文件夹结构

发布于 2024-10-21 14:55:25 字数 1432 浏览 1 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

少女情怀诗 2024-10-28 14:55:25

MVC 模式与应用程序的文件夹布局完全没有关系。

无论您将所有文件放入一个文件夹还是使用问题中所示的布局都是完全无关的。它并没有从中获得更多或更少的 MVC,因为 MVC 不是关于文件夹,而是关于分割用户界面交互分为三个不同的角色

除非您遵循需要特定文件命名方案的代码约定(例如 PEAR),否则对您的应用程序来说唯一重要的是您的自动加载器可以在运行时以某种方式找到文件。因此,如果您认为上面显示的布局适合您,请使用它。

罗伯特“鲍勃叔叔”马丁建议您的文件夹布局应该表达该应用程序是关于什么的。

您的架构应该告诉读者有关系统的信息,而不是您在系统中使用的框架。如果你正在构建一个医疗保健系统,那么当新程序员查看源存储库时,他们的第一印象应该是:“哦,这是一个医疗保健系统”。那些新程序员应该能够学习系统的所有用例,但仍然不知道系统是如何交付的。他们可能会来找你说:“我们看到一些看起来有点像模型的东西,但是视图和控制器在哪里”,你应该说:“哦,这些是目前不需要你关心的细节,我们稍后会向您展示。”

The MVC pattern has absolutely nothing to do with your application's folder layout.

Whether you put all your files into one single folder or use a layout like shown in your question is completely irrelevant. It's not getting more or less MVC from it, because MVC is not about folders, but about splitting user interface interaction into three distinct roles.

Unless you are following a code convention that demands a certain file naming scheme (like PEAR), the only thing important for your application is that your Autoloader can find the files at runtime somehow. So if you think the layout shown above is good for you, go with it.

Robert "Uncle Bob" Martin suggests that your folder layout should express what the application is about.:

Your architectures should tell readers about the system, not about the frameworks you used in your system. If you are building a health-care system, then when new programmers look at the source repository, their first impression should be: “Oh, this is a heath-care system”. Those new programmers should be able to learn all the use cases of the system, and still not know how the system is delivered. They may come to you and say: “We see some things that look sorta like models, but where are the views and controllers”, and you should say: “Oh, those are details that needn’t concern you at the moment, we’ll show them to you later.”

难以启齿的温柔 2024-10-28 14:55:25

我正在使用类似的结构(也使用自制框架,但从网络根目录备份)。您可以在私人文件夹中添加一个“表单”文件夹。

我用它来使控制器更具可读性。这些表单通常是一堵巨大的目标代码墙。将它们放入控制器中包含的外部文件中是一个好主意。

不要忘记从重写规则中排除 public 文件夹,一切都应该没问题:)

另一种解决方案是将 index.php 放在你的 public 文件夹中,并将该文件夹定义为 nginx 中的 webroot。它防止远程访问仅应由框架使用的所有其他文件(例如备份文件)。

/applications
    /administration
        /private
            /controllers
            /models
            /views
            configuration.php
        /public <---- Vhost WebRoot
            /ajax
            /fonts
            /icons
            /images
            /stylesheets
            index.php
    /website
        /private
            /controllers
            /models
            /views
            configuration.php
        /public <---- Vhost WebRoot
            /ajax
            /fonts
            /icons
            /images
            /stylesheets
            index.php
/backups
/library
    /helpers
        datetime.php
        text.php
    controller.php
    model.php

I'm using a similar structure (with homemade framework too but backup out of webroot). You could maybe add a "form" folder in the private folder.

I use this to make controller more readable. The forms are generaly a big wall of object code. Putting them in an external file included in the controller is a good idea.

Don't forget to exclude the public folder from the rewriting rules and everything should be allright :)

An other solution is to put index.php in your public folder and define this folder as your webroot in nginx. It prevent remote access to all other file (like backup file) which should used only by the framework.

/applications
    /administration
        /private
            /controllers
            /models
            /views
            configuration.php
        /public <---- Vhost WebRoot
            /ajax
            /fonts
            /icons
            /images
            /stylesheets
            index.php
    /website
        /private
            /controllers
            /models
            /views
            configuration.php
        /public <---- Vhost WebRoot
            /ajax
            /fonts
            /icons
            /images
            /stylesheets
            index.php
/backups
/library
    /helpers
        datetime.php
        text.php
    controller.php
    model.php
维持三分热 2024-10-28 14:55:25

快速说明:我会远离公共/私人文件夹,因为您基本上将自己锁定在两个角色中。在这种情况下,ACL 的实现会很困难/令人困惑。

Quick note: I would stay away from public/private folders as you're essentially locking yourself into two roles. An ACL implementation would be difficult/confusing in this situation.

时光清浅 2024-10-28 14:55:25

我想管理和网站是网站的不同模块。

为什么你没有共享或核心模块文件夹。例如,您有一个名为“User”的数据库模型和名为 createUser()、editUser()、listUsers()、changeRightsOfUser() 等的方法。使用此结构,您需要为所有模块编写和抽象此模型。

您的控制器必须是唯一的,这没关系。但模型对于每个模块都是可用或可扩展的。

I guess administration and website is different modules of website.

Why dont you have a shared or core modules folder. For example, you have a database model named "User" and methods named createUser(), editUser(), listUsers(), changeRightsOfUser() and more more.. With this structure you need to write and abstract this model for all modules.

Your controllers is must be unique, this is okey. But models can be usable or extendable for every module.

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