组织 PHP 项目

发布于 2024-11-15 16:06:55 字数 2310 浏览 3 评论 0原文

它是什么

这是我到目前为止所做的:

  • 核心/
    • 控制器/ (包含应用使用的控制器)
    • 模型/ (包含应用使用的模型)
    • 视图/ (包含应用使用的视图)
    • base_controller.php (每隔一个扩展的控制器)
    • base_model.php (每隔一个扩展的模型)
  • 供应商/
    • phprouter/(一个简单的路由器类)
    • pimple/(一个简单的 DI 容器类)
  • configuration.php (包含所有应用程序配置)
  • index.php (包括配置、供应商、基本模型、基本控制器、设置 DI 容器并路由请求)

请参阅此处的代码:http://pastebin.com/pxUpUvv6
请注意,给定的代码只是一个示例,因此控制器、模型、视图尚未到位。另外,它可能存在错误(未经测试),但现在并不重要。

请求流程

  1. 客户端请求 index.php
  2. 包括配置、供应商、基本控制器、基本型号。
  3. DI 容器和依赖项已初始化,我们现在可以将它们注入到任何地方。
  4. 我们将控制器映射到 URL,然后路由器完成其工作。
  5. 控制器被获取(尽管这不在示例代码中,如上所述)。
    • 我们做了一些事情。
    • 然后该方法调用 ::call_model(),其中包含来自 core/models/ 的相应模型,然后调用我们在对应的模型类。
  6. 模型已获取。
    • 更多内容。
    • 模型然后调用 ::call_view()',其中包括来自 core/views/ 的相应视图。
  7. 获取视图并将页面呈现给客户端。

仅供参考:

控制器、模型、视图的对应示例:

  • 控制器 Controller_Products::list() at core/controllers/Controller_Products.php
  • 模型 Model_Products::list () as core/models/Model_Products.php
  • 查看 core/views/Model_Products_list.php

实际上面临的问题

,我觉得对这个结构有点不舒服。不知道,它似乎离可扩展、可模块化还很远……

  1. 是否基本的文件夹结构 - core{, /controllers, /models/, /views }vendors 位于根部 - 您觉得不错吗?
  2. 我觉得我应该在 index.php 之外获取 __autoload() ,这对我来说似乎有点太大了。如果是这样,那么 DI 容器呢?
  3. 也许如果我需要两个以上的外部库,最好不要手动将它们一一包含在内?但如何呢?
  4. 在我看来,将所有配置放在根目录下的文件 configuration.php 中就像老式的 PHP4。感谢 Pimple 的强大功能,我可以将此配置直接嵌入其中,但是在哪里?
  5. 我认为我处理 ::call_model() (core/base_controller.php) 和 ::call_view() (core /base_model.php)有点尴尬。你同意吗?重做整个事情的简化方法是什么?
  6. 考虑到我的所有问题,最终对我来说使用 Symfony 这样的框架会更好吗?

如果有不清楚的地方,请随时询问。
谢谢。

What It Is

Here is what I've done so far:

  • core/
    • controllers/ (contains the controllers used by the app)
    • models/ (contains the models used by the app)
    • views/ (contains the views used by the app)
    • base_controller.php (the controller every other extend)
    • base_model.php (the model every other extend)
  • vendors/
    • phprouter/ (a simple router class)
    • pimple/ (a simple DI container class)
  • configuration.php (contains all the app configuration)
  • index.php (includes the configuration, vendors, base model, base controller, sets the DI container up and route the request)

See the code here: http://pastebin.com/pxUpUvv6
Please note that the given code is just an example, therefore the controllers, models, views aren't in place yet. Also, it may be buggy—as untested—, but it doesn't matter right now.

Request Flow

  1. The client requests index.php.
  2. The configuration, vendors, base controller, base model are included.
  3. The DI container and the dependencies are initialized, we can now inject them anywhere.
  4. We map controllers to URL and the router does its job.
  5. The controller is fetched (although this is not in the example code, as noted above).
    • We do some stuff.
    • The method then calls ::call_model(), which includes the corresponding model from core/models/, and then calls the same method we're using from the model class corresponding.
  6. The model is fetched.
    • More stuff.
    • The model then calls ::call_view()', which includes the corresponding view from core/views/.
  7. The view is fetched and render the page to the client.

FYI: Corresponding

Examples of controller, model, view which correspond:

  • Controller Controller_Products::list() at core/controllers/Controller_Products.php
  • Model Model_Products::list() as core/models/Model_Products.php
  • View at core/views/Model_Products_list.php

Issues Being Faced

Actually, I feel a bit uncomfortable with this structure. Dunno, it seems to be far from scalable, modulable...

  1. Does only the basic folder structure—core{, /controllers, /models/, /views}, vendors at the root—looks good to you?
  2. I feel like I should get __autoload() outside of index.php, which seems a little too big to me. If so, what about DI container?
  3. Maybe if I get to needing more than two external library, it should be better not to have them included one by one, manually? But how?
  4. Putting all the configuration in a file configuration.php at the root looks to me like old-fashioned PHP4. Thanks to the power of Pimple, I could embed this configuration directly into it but yet, where?
  5. I think the way I handle ::call_model() (core/base_controller.php) and ::call_view() (core/base_model.php) is a bit awkward. Would you agree? What'd be a simplified way to redo the whole thing?
  6. Considering all my issues, would it eventually be better for me to use a framework as Symfony?

If something isn't clear, feel free to ask.
Thanks.

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

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

发布评论

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

评论(1

跨年 2024-11-22 16:06:55
  1. 是的。
  2. 您可以同时使用自动加载和 DI 容器。 有示例,如何自动加载可以与命名约定一起使用。我建议使用 spl_autoload。
  3. 通过自动加载,您可以删除所有(或几乎所有)包含内容。
  4. 我猜是在index.php 中。
  5. 是的,方法不对。首先,尽量不要使用静态方法。此外,模型应该具有具有描述性名称的方法,而不仅仅是“打电话给我,我会尽我所能”。这是更复杂的问题 - 您需要了解控制器和模型应该如何进行合作。作为变体,读一些书。控制器应该调用模型的方法来获取某些情况的数据。模型不仅仅是控制器代码的地方。不同的控制器可以使用不同的模型。模型也可以使用其他模型。
  6. 这个问题的回答不能客观:)
  1. Yes.
  2. You can use autoload and DI container together. There is example, how autoload can be used with naming convention. I recommend to use spl_autoload.
  3. With autoload you can remove all (or almost all) includes.
  4. In index.php, I guess.
  5. Yes, it's wrong way. First of all, try to not use static methods. Also, models should have methods with descriptive names, not just 'call me and I will do all what I can'. It's more complex problem - you need to understand how Controller and Model should do their cooperation. As variant, read some books. Controller should call methods of Model, to get data for some situation. Model it's not just place for code of controller. Different controllers can use different models. Models too can use another models.
  6. Answer to this question can not be objective :)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文