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

查看:80
本文介绍了在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.

现在,如果逻辑被放到模型中,它工作得很好,直到我需要使用其他型号。当然这是可能的,但是当与在控制器中加载模型相比时,这不是那么简单。这就像一个蛋糕开发者告诉我当然,如果你想这样做,这是可能的,但这不是我该怎么做。

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.

我到目前为止已经设法避免它(主要是由于缺乏勇气),但是

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.

另一个可能的解决方案是 有限状态机行为 ,允许您将逻辑推向模型更干净,并围绕它构建您的Controller +组件。此行为的唯一后备(IMHO)是它不允许在单个模型上的多个状态字段。

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天全站免登陆