组织 PHP 项目
它是什么
这是我到目前为止所做的:
- 核心/
- 控制器/ (包含应用使用的控制器)
- 模型/ (包含应用使用的模型)
- 视图/ (包含应用使用的视图)
- base_controller.php (每隔一个扩展的控制器)
- base_model.php (每隔一个扩展的模型)
- 供应商/
- phprouter/(一个简单的路由器类)
- pimple/(一个简单的 DI 容器类)
- configuration.php (包含所有应用程序配置)
- index.php (包括配置、供应商、基本模型、基本控制器、设置 DI 容器并路由请求)
请参阅此处的代码:http://pastebin.com/pxUpUvv6
请注意,给定的代码只是一个示例,因此控制器、模型、视图尚未到位。另外,它可能存在错误(未经测试),但现在并不重要。
请求流程
- 客户端请求 index.php。
- 包括配置、供应商、基本控制器、基本型号。
- DI 容器和依赖项已初始化,我们现在可以将它们注入到任何地方。
- 我们将控制器映射到 URL,然后路由器完成其工作。
- 控制器被获取(尽管这不在示例代码中,如上所述)。
- 我们做了一些事情。
- 然后该方法调用
::call_model()
,其中包含来自 core/models/ 的相应模型,然后调用我们在对应的模型类。
- 模型已获取。
- 更多内容。
- 模型然后调用
::call_view()
',其中包括来自 core/views/ 的相应视图。
- 获取视图并将页面呈现给客户端。
仅供参考:
控制器、模型、视图的对应示例:
- 控制器
Controller_Products::list()
at core/controllers/Controller_Products.php - 模型
Model_Products::list ()
as core/models/Model_Products.php - 查看 core/views/Model_Products_list.php
实际上面临的问题
,我觉得对这个结构有点不舒服。不知道,它似乎离可扩展、可模块化还很远……
- 是否仅基本的文件夹结构 -
core{, /controllers, /models/, /views }
、vendors
位于根部 - 您觉得不错吗? - 我觉得我应该在 index.php 之外获取
__autoload()
,这对我来说似乎有点太大了。如果是这样,那么 DI 容器呢? - 也许如果我需要两个以上的外部库,最好不要手动将它们一一包含在内?但如何呢?
- 在我看来,将所有配置放在根目录下的文件 configuration.php 中就像老式的 PHP4。感谢 Pimple 的强大功能,我可以将此配置直接嵌入其中,但是在哪里?
- 我认为我处理
::call_model()
(core/base_controller.php) 和::call_view()
(core /base_model.php)有点尴尬。你同意吗?重做整个事情的简化方法是什么? - 考虑到我的所有问题,最终对我来说使用 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
- The client requests index.php.
- The configuration, vendors, base controller, base model are included.
- The DI container and the dependencies are initialized, we can now inject them anywhere.
- We map controllers to URL and the router does its job.
- 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.
- The model is fetched.
- More stuff.
- The model then calls
::call_view()
', which includes the corresponding view from core/views/.
- 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...
- Does only the basic folder structure—
core{, /controllers, /models/, /views}
,vendors
at the root—looks good to you? - 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? - 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?
- 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?
- 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? - 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)