下面是在 CodeIgniter 应用程序中实现服务层的两种方式。
第一种方法
1.send request to the controller
2.calling service layer methods from controller
3.return processed result data set(D1) from service layer to controller
4.then according to that data set controller demand data from model
5.model return data set(D2) to the controller
6.then controller send second data set(D2) to view.
第二种方法
1.send request to the controller
2.calling service layer methods from controller
3.service layer demand data from model
4.model send requested data set(d1) to the service layer
5.after some processing return generated data(d2) to controller from service layer
6.then controller send data set(d2) to view.
在 CodeIgniter 中实现服务层的正确方法是什么?除了这两种方法,还有其他好的方法吗?
如果你能在代码中提供一个例子,那就太好了
最佳答案
Please note, this is not necessarily the correct way of doing it, but I'm going to explain how a framework like might typically do it and then you can learn about other methods and decide the best one for your use-case. Therefore, I do not expect this answer to be the correct one, but I hope it imparts at least a little knowledge on how things are done before someone who actually knows what they're talking about comes along and chimes in (also to those people - please feel free to edit / downvote this answer :D). Finally this also has nothing to do with CodeIgniter but frameworks in general. Your question should not only be framed as framework-agnostic, but language-agnostic also.
所以我将在这里提出一个观点,即所有现代框架,特别是 PHP,不做MVC .为什么这很重要?因为我们都需要说同一种语言,而 PHP 中不存在“mvc”。这是事实。接受这一点,然后我们可以继续将框架使用的概念混为一谈; CodeIgnitor 是“MVC” SCSS 化的一个特别好的例子;此后称为带引号的“mvc”。
好的一面是,例如像 Symfony 这样的框架,提供了一个初始的自以为是的架构,它至少包含跨应用程序的某种形式的一致性,它是这样的:
app.php
或 app_dev.php
取决于您是否处于开发或生产阶段;一个涉及需要在每次更改时运行的大量缓存,而另一个则不涉及 - 这对于开发来说是完美的。 Repositories
, Services
等等(并将这些标量传递给它们,还记得分离吗?),它们执行业务逻辑,(通常这些是您的设计模式在日常工作中发挥作用的地方),然后将该数据返回给应用程序-服务水平。 Note, Fabien Potencier explicitly states that Symfony was not an MVC framework - at least he knows what he's talking about there. Again, this is not purist, it's important we're all speaking the same, factually correct language in computing.
所以,因为你非常喜欢图表,下面是一些人如何用今天的框架来做...
这是针对具有
Review
概念的应用程序。和 Criteria
每个Review
.甚至不要让我开始使用 Symfony 表单,它们与所有事物如此耦合,它们并不是任何架构的重要组成部分。你需要多少效果层?我们已经有了“MVC”,在 DDD 中我们有“应用程序”、“域”和“基础设施”分离的概念,所以先让这两者一起工作,然后是这个“服务层”?你真的需要另一层,还是上面的就够了?需要考虑的事情...
看,由于这种分离,您并没有被框架/http 请求困住来使应用程序运行。
看到上图中的“服务”了吗?它们与 Controller 分离,因此您可以从任何地方抛出标量,应用程序仍然可以工作。 我认为这会给你你需要的分离 .以正确的方式做事,学习如何做,然后学习如何控制自己并在涉及业务和需求时务实,这是很棒的,但是框架需要整理它们的东西 - 你当然不会使用 CodeIgniter 编写可爱的代码;)
关于php - 在 CodeIgniter 应用程序中实现服务层的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40452245/