MVC 设计 - CodeIgniter MVC Web 应用程序项目中可以/应该/必须有多少个控制器?

MVC Design - How many controllers can/should/must I have in a CodeIgniter MVC web app project?(MVC 设计 - CodeIgniter MVC Web 应用程序项目中可以/应该/必须有多少个控制器?)
本文介绍了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 应用程序项目中可以/应该/必须有多少个控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)