正确使用 MVC (CakePHP) 控制器
Cake 的文档称“最常见的是,控制器用于管理单个模型的逻辑。”我发现这对于我的大多数代码来说是不常见的,并且我不想打破约定,除非这样做是正确的。
例如,我的应用程序在用户登录后将其发送到他们的帐户仪表板 - 这使用了大约六个表中的数据,但并非所有表都是相关的。我是否为此创建一个“仪表板”控制器(即使没有仪表板模型或表格)?或者我是否在现有控制器中创建仪表板方法?
谢谢,布莱恩
Cake's documentation says "Most commonly, controllers are used to manage the logic for a single model." I'm finding this is uncommon for most of my code, and I don't want to break convention unless it is proper to do so.
For example, my application sends a user to their account dashboard after they log in - this uses data from probably half a dozen tables, not all of which are even related. Do I create a "dashboard" controller for this (even though there is no dashboard model or table)? Or do I create a dashboard method in an existing controller?
Thanks, Brian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我有类似的情况,我的处理方法是将连接许多模型的操作保留在最中心的控制器中。例如,我的用户可以创建语音注释、评论、设置、Twitter 和 Facebook 信息。例如,我可以从我的用户模型
$this->User->Voicenotes->find('all')
获取所有这些信息。我相信创建额外的控制器可能会让您感到困惑,使用 cake 为您提供的内容,您可以通过设置 $uses 变量或使用 loadModel 来指定在控制器中使用模型code> 在控制器操作中,如果您设置了关系,则可以按照我之前描述的方式进行操作,无需创建额外的控制器。
我想这取决于您希望自己的应用程序如何工作以及在您的情况下什么更容易。
I have a similar situation and how I handle it is keeping the actions that connect a lot of models in the controller that is the most centric. For instance, my user can create voicenotes, comments, has settings, has twitter and facebook information. All this information I can get from my user model
$this->User->Voicenotes->find('all')
, for example.I believe creating additional controllers might just confuse you, use what cake gives you, you can specify that models are to be used in a controller either by setting the
$uses
variable or usingloadModel
in the controller action, if you have your relations set up you can just do it the way i described before, no need to create additional controllers.I guess it depends on how you want your own app to work and what comes easier in your situation.