問題描述
我正在使用 CodeIgniter MVC 框架在 PHP 中構(gòu)建一個相對簡單的 Web 應(yīng)用程序.我以前開發(fā)過 PHP 應(yīng)用程序,但從未以嚴格的方式開發(fā)過.我已經(jīng)習慣了 MVC 框架,但是有兩個問題一直讓我回到繪圖板,它拖慢了開發(fā)速度.
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.
是否有關(guān)于 Web 應(yīng)用程序可以擁有多少個控制器的最佳實踐?我的應(yīng)用程序有一個(相當大的)控制器,其中包括業(yè)務(wù)邏輯、表單提交邏輯等.雖然這有效,但我正在爭論是否有單獨的控制器,例如一個用于表單處理和提交,另一個用于用戶管理(會話、登錄、注冊)等.再說一次,我也不想為此過度設(shè)計.我被掛斷的部分是: 控制器有一個默認"索引函數(shù),可以在父 URL 上加載.所有的業(yè)務(wù)邏輯都從這里開始(例如,表單提交、處理數(shù)據(jù)等).隨著開發(fā)的繼續(xù),我默認只在同一個控制器中創(chuàng)建新功能.它有效,但我的部分目標是同時學習最佳實踐,有些東西告訴我我可能會以錯誤的方式進行.
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.
模型應(yīng)該僅用于數(shù)據(jù)庫讀/寫功能還是我可以在其中放置幫助"功能,例如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)
模型應(yīng)該是整個應(yīng)用程序的骨干.模型應(yīng)該處理所有業(yè)務(wù)邏輯和數(shù)據(jù)庫管理.同時,控制器應(yīng)該盡可能的細化,只真正提供模型和視圖之間的接口.
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.
例如,在登錄屏幕中,控制器應(yīng)該為用戶提供登錄視圖.當用戶輸入他的信息時,控制器應(yīng)該處理輸入驗證并將輸入轉(zhuǎn)發(fā)給模型,模型應(yīng)該以成功"或失敗"作為響應(yīng).因此,控制器應(yīng)該將用戶重定向到儀表板,或者將用戶發(fā)送回帶有錯誤消息的登錄屏幕 - 分別.
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.
總結(jié):模型應(yīng)該是胖的,控制器應(yīng)該是瘦的.
這篇關(guān)于MVC 設(shè)計 - CodeIgniter MVC Web 應(yīng)用程序項目中可以/應(yīng)該/必須有多少個控制器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!