组织一个PHP项目 [英] Organizing a PHP project

查看:63
本文介绍了组织一个PHP项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here is what I've done so far:

  • 核心/
    • 控制器/ (包含应用程序使用的控制器)
    • 模型/ (包含应用程序使用的模型)
    • 视图/ (包含应用程序使用的视图)
    • base_controller.php (控制器互相扩展)
    • base_model.php (该模型每隔一个扩展)
    • 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)
      • phprouter/(一个简单的路由器类)
      • pimple/(一个简单的DI容器类)
      • phprouter/ (a simple router class)
      • pimple/ (a simple DI container class)

      在此处查看代码: http://pastebin.com/pxUpUvv6
      请注意,给定的代码只是一个示例,因此尚无控制器,模型,视图.另外,它可能是越野车(未经测试),但现在并不重要.

      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.

      1. 客户端请求 index.php .
      2. 包括配置,供应商,基本控制器,基本模型.
      3. DI容器和依赖项已初始化,我们现在可以将它们注入到任何地方.
      4. 我们将控制器映射到URL,然后路由器完成其工作.
      5. 已获取控制器(尽管如上所述,这不在示例代码中).
        • 我们做些事情.
        • 然后该方法调用::call_model(),其中包括 core/models/中的相应模型,然后从相应的模型类中调用我们正在使用的相同方法.
      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.
      • 更多东西.
      • 然后模型调用::call_view()',其中包括 core/views/中的相应视图.
      • More stuff.
      • The model then calls ::call_view()', which includes the corresponding view from core/views/.

      仅供参考:

      与之对应的控制器,模型,视图的示例:

      FYI: Corresponding

      Examples of controller, model, view which correspond:

        位于 core/controllers/Controller_Products.php
      • 控制器Controller_Products::list()
      • 将模型Model_Products::list()作为 core/models/Model_Products.php
      • core/views/Model_Products_list.php
      • 上查看
      • 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

      实际上,我对这种结构感到不舒服.邓诺(Dunno),它似乎远非可扩展,可调节...

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

      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最终会更好吗?
      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.

      推荐答案

      1. 是的.
      2. 您可以同时使用自动加载和DI容器. 有例子,如何autoload可以与命名约定一起使用.我建议使用spl_autoload.
      3. 使用自动加载功能,您可以删除所有(或几乎所有)包含文件.
      4. 我猜是在index.php中.
      5. 是的,这是错误的方式.首先,尝试不使用静态方法.此外,模型应该具有带有描述性名称的方法,而不仅仅是打电话给我,我将尽我所能".这是一个更复杂的问题-您需要了解Controller和Model应该如何进行协作.作为变体,请阅读一些书籍.控制器应调用Model的方法,以获取某些情况下的数据.建模不仅是放置控制器代码的地方.不同的控制器可以使用不同的模型.模型也可以使用其他模型.
      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 :)

      这篇关于组织一个PHP项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆