在 CakePHP 中共享应用程序逻辑的正确位置是什么? [英] What's the correct place to share application logic in CakePHP?

查看:34
本文介绍了在 CakePHP 中共享应用程序逻辑的正确位置是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这个问题的简单答案是组件.

I guess simple answer to the question would be a component.

虽然我同意,但我觉得必须为如此具体的东西编写一个组件很奇怪.

Although I agree, I feel weird having to write a component for something so specific.

例如,假设我有一个用户表.当一个用户被创建时,它应该形成一个事件的连锁反应,在数据库周围启动与用户相关的不同类型的数据.我认为最好避免直接从不同的控制器操作数据库,而是将所有这些都整齐地打包在一个方法中.然而,由于某些逻辑需要单独访问,我真的不能在一个方法中拥有整个包.相反,我认为将它分解成更小的部分是合乎逻辑的(比如 $userModelOrController->createNew()$candyStorageModelOrController->createNew())与各自的数据库表进行交互.

For example, let's say I have a table of users. When a user is created, it should form a chain reaction of events, initiating different kinds of data related to the user all around the database. I figured it would be best to avoid directly manipulating the database from different controllers and instead pack all that neatly in a method. However since some logic needs to be accesed separately, I really can't have the whole package in a single method. Instead I thought it would be logical to break it up to smaller pieces(like $userModelOrController->createNew() and $candyStorageModelOrController->createNew()) that only interact with their respective database table.

现在,如果将逻辑放入模型中,它会很好地工作,直到我需要使用其他模型.当然这是可能的,但与在控制器中加载模型相比,它并不是那么简单.这就像一个 Cake 开发人员告诉我当然,如果你想那样做是可能的,但我不会那样做".

Now, if the logic is put to the model, it works great until I need to use other models. Of course it's possible, but when compared to loading models in a controller, it's not that simple. It's like a Cake developer telling me "Sure, it's possible if you want to do it that way but that's not how I would do it".

然后,如果将逻辑放入控制器,我可以通过 $this->loadModel() 访问其他模型,但这让我回到之前解释的情况,因为我需要能够无限期地继续连锁反应.从控制器访问其他控制器是可能的,但似乎没有任何直接的方法可以这样做,所以我猜我仍然没有做对.

Then, if the logic is put to the controller, I can access other models really easy through $this->loadModel(), but that brings me back to the previously explained situation since I need to be able to continue the chain reaction indefinitely. Accessing other controllers from a controller is possible, but again there doesn't seem to be any direct way of doing so, so I'm guessing I'm still not doing it right.

通过使用组件可以轻松解决这个问题,因为组件可用于我想要的每个控制器.但是就像我在开头写的那样,专门为这个任务创建一个组件感觉很尴尬.对我来说,组件看起来更像是额外功能的包(比如核心组件),而不是共享控制器特定逻辑的东西.

By using a component this problem could be solved easily, since components are available to every controller I want. But like I wrote at the beginning, it feels awkward to create a component specifically for this one task. To me, components seem more like packages of extra functionality(like the core components) and not something to share controller-specific logic.

由于我对整个 MVC 事物不熟悉,因此我可能完全误解了这个概念.再一次,如果有人指出我正确的方向,我将不胜感激:)

Since I'm new to this whole MVC thing, I could've completely misunderstood the concept. Once again, I would be thankful if someone pointed me to the right direction :)

推荐答案

每当我面对如此困难的场景时,我都会盯着 CakePHP 事件系统 无限小时.

Whenever I am faced with such difficult scenarios, I stare at CakePHP Events System for an infinite number of hours.

到目前为止,我设法以某种方式避免它(主要是因为缺乏勇气),但我认为值得研究一下,因为它显然是为了跳过 MVC墙"而设计的在某种程度上.

I've so far managed to avoid it somehow (mostly due to lack of courage), but I think it's worth looking into, since it is obviously designed to jump the MVC "walls" to some extent.

另一个可能的解决方案是 Finite状态机行为,它允许您将逻辑更清晰地推向模型并围绕它构建您的控制器+组件.此行为的唯一后备(恕我直言)是它不允许单个模型上有多个 state 字段.

Another possible solution is the Finite State Machine behavior, which allows you to push the logic towards the Model more cleanly and build your Controller+Component around it. The only fallback (IMHO) of this Behavior is that it doesn't allow for multiple state fields on a single Model.

对此类问题进行更真实的表述可能会产生更好的解决方案,因为我们中的许多人都走上了这条路,这可能是一个有价值的例子.

A more factual representation of such a problem might yield better solutions, since many of us went down this road and it could be a valuable example.

这篇关于在 CakePHP 中共享应用程序逻辑的正确位置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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