MVC 设计 - CodeIgniter MVC Web 应用程序项目中可以/应

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

问题描述

我正在使用 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 应用程序可以拥有多少个控制器的最佳实践?我的应用程序有一个(相当大的)控制器,其中包括业务逻辑、表单提交逻辑等.虽然这有效,但我正在争论是否有单独的控制器,例如一个用于表单处理和提交,另一个用于用户管理(会话、登录、注册)等.再说一次,我也不想为此过度设计.我被挂断的部分是: 控制器有一个默认"索引函数,可以在父 URL 上加载.所有的业务逻辑都从这里开始(例如,表单提交、处理数据等).随着开发的继续,我默认只在同一个控制器中创建新功能.它有效,但我的部分目标是同时学习最佳实践,有些东西告诉我我可能会以错误的方式进行.

  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.

模型应该用于数据库读/写功能还是我可以在其中放置帮助"功能,例如generate_random_number、validate_login_credentials、login_session、logout_session 等

Should models only be used for database read/write functions or can I put "helper" functions in there as well, e.g. generate_random_number, validate_login_credentials, login_session, logout_session, etc.

推荐答案

(这是我的主观意见 - 它对我很好)

(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 Web 应用程序项目中可以/应该/必须有多少个控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:熟悉 MVC - 我如何使用会话逻辑、附加类和后台逻 下一篇:如何在我的 PHP/SQL/HTML/CSS 代码上实现 MVC 风格?

相关文章

最新文章