在 CakePHP 中将某些逻辑放在哪里

发布于 2024-08-21 10:16:35 字数 396 浏览 10 评论 0原文

我最近开始使用 CakePHP 重写几年前做的一个项目。这次我试图把所有事情都做“正确”,所以也许有人会给我一个关于执行以下操作的指示:

我正在使用 Model->find('all') 中的表中显示一个简单的表看法。该表中有两个布尔字段,它们共同构成了我需要向用户显示的内容。所以:0x0 = 'A',1x0 = 'B',0x1 = 'C',1x1 = 'D'。我应该把这个逻辑放在哪里?我一直在考虑以下方法:

  1. 视图
  2. 视图助手
  3. 控制器
  4. 模型中的某些内容,以便 Model->find('all') 输出该值(这可能吗?)

这个任务可能看起来微不足道,但是我认为这可能会让我从一开始就让这个项目变得有组织和可维护。

谢谢!

I've recently started to rewrite a project I did a few years ago using CakePHP. I'm trying to do everything 'right' this time, so maybe someone get give me a a pointer on doing to the following:

I'm showing a simple table from a table using Model->find('all') in the View. There are two boolean fields in this table, that together make up something I need to show to a user. So: 0x0 = 'A', 1x0 = 'B', 0x1 = 'C', 1x1 = 'D'. Where should I put this logic? I've been thinking about the following methods:

  1. The view
  2. A View helper
  3. The controller
  4. Something in the Model so that Model->find('all') outputs this value (is that even possible?)

This task might seem trivial, but I think it might learn me getting this project organized and maintainable from the start.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遗忘曾经 2024-08-28 10:16:35

嗯,这取决于组成决赛桌的逻辑类型(是演示还是业务?)。

想象一下您添加了新类型的 UI,例如命令行界面。你会如何在那里展示你的桌子?对于 HTML 和控制台演示,传递到 View 的数据必须相同。因此,负责准备数据的逻辑是业务逻辑,它应该放置在模型中。负责显示数据的逻辑应该放置在视图中(如果多次使用,则可以放置在视图助手中)。

并且永远不要将这种逻辑放在 Controller 中。

Well, it depends on the type of logic for making up final table (is it presentation or business?).

Imagine you add new type of UI, for example command line interface. How would you show your table there? The data passed to View has to be same for both HTML and console presentations. So the logic which is responsible for preparing that data - is business logic and it should be placed in Model. The logic responsible for displaying the data should be placed in View (maybe in view helper if it's used more than once).

And never place this kind of logic in Controller.

梦与时光遇 2024-08-28 10:16:35

如果您要在各处使用它,我会将其放入模型中。您可以在模型上放置一个返回该值的方法,也可以循环遍历 afterFind 回调并将其设置为正确的字段。

If it's something you're going to use all over the place I would put it in the model. You can either put a method on the model that gives that value back or loop over all the rows you've retrieved in an afterFind callback and set it as a proper field.

染火枫林 2024-08-28 10:16:35

如果这种逻辑将决定渲染风格,我会将这种逻辑放入视图中。这样,设计师就有最大的访问权限并可以相应地设计风格。

另一方面,如果这两列只是为了方便数据建模而存在,则将其放入模型中。设计师甚至不应该意识到其他可能性!

I put this kind of logic in the view if it is something that is going to determine rendering style. In that way, the designer has maximum access and can style accordingly.

On the other hand, if the two columns only exist for convenience in datamodelling, put it in the model. The designer shouldn't even be aware of other possibilities!

北渚 2024-08-28 10:16:35

在控制器中!模型中的方法位于控制器中。视图仅用于输出(如 HTML UI 编程。)

In the controller! The methods from the model comes in the controller. The view is just for output( like HTML UI programming.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文