MVC设计 - 在CodeIgniter MVC网络应用项目中我可以/应该/必须有多少控制器? [英] MVC Design - How many controllers can/should/must I have in a CodeIgniter MVC web app project?

查看:64
本文介绍了MVC设计 - 在CodeIgniter MVC网络应用项目中我可以/应该/必须有多少控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CodeIgniter MVC框架在PHP中构建一个相对简单的Web应用程序。我已经开发PHP应用程序,但从来没有规范的方式。我习惯了MVC框架,但是两个问题仍然让我回到绘图板,并减缓开发速度。

I'm building a relatively simple web application in PHP using the CodeIgniter MVC framework. I've developed PHP applications before but never in a disciplined manner. I'm getting used to the MVC framework, however two questions keep bringing me back to the drawing board and it's slowing down development.


  1. 有一个web应用程序可以有多少控制器的最佳实践?我有一个(相当大的)我的应用程序的控制器,包括业务逻辑,表单提交逻辑等。虽然这工作,我辩论是否有意义,有独立的控制器,例如。一个用于表单处理和提交,另一个用于用户管理(会话,登录,注册)等。然后,再次,我不想对此进行overengineer。 我要挂断的部分是:控制器具有在父网址加载的默认索引函数。所有的业务逻辑从这里开始(例如,表单提交,对数据做某事等)。我默认只是在同一控制器创建新功能,开发继续。

  1. Is there a best practice on how many controllers a web app can have? I have one (fairly large) controller for my app that includes business logic, form submission logic, etc. While this works, I'm debating whether it would make sense to have separate controllers, e.g. one for form handling and submission, another for user management (session, login, sign up), etc. Then again, I don't want to overengineer the heck out of this either. The part where I am getting hung up is: the controller has a "default" index function that gets loaded at the parent URL. All the business logic starts off from here (e.g., form submission, doing something with the data, etc.). I've defaulted to just creating new functions in the same controller as development continued. It works, but part of my goal is to also learn the best practice and something tells me I may be going about this the wrong way.

应该模型


推荐答案

我的主观意见 - 它已经对我很好)

(This is my subjective opinion - it has treated me well)

模型应该是整个应用程序的肉和骨头。模型应该处理所有业务逻辑和数据库管理。同时,控制器应该尽可能的薄,只是真正提供模型和视图之间的接口。

Models should be the meat and bone of your entire applications. The models should handle all business logic and database management. Meanwhile, the controllers should be as thin as possible, only really providing an interface between the model and view.

例如,在登录屏幕中,控制器应提供用户与登录视图。当用户输入他的信息时,控制器应处理输入验证并将输入转发到模型,模型应该响应成功或失败。因此,控制器应将用户重定向到信息中心,或将他们发送回登录屏幕并分别显示错误消息。

For instance, in a login screen, the controller should provide the user with the login view. When the user inputs his information, the controller should handle input validation and forward the input to the model, which should respond with "success" or "failure". Consequently the controller should redirect the user to the dashboard, or send him back to the login screen with an error message - respectively.

总结:模型应脂肪,控制器应该很薄。

这篇关于MVC设计 - 在CodeIgniter MVC网络应用项目中我可以/应该/必须有多少控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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