問題描述
我試圖理解模型上的胖"與控制器上的瘦"的概念,并根據我一直在討論的內容,我有以下示例(摘自 freenode 討論):
I'm trying to understand the concept of "Fat" on models vs "skinny" on controllers and from what I've been discussing I have the following example (this is taken from a freenode discussion):
Q:在 MVC 范式上,它說的是胖模型,瘦控制器.我在這里想,如果我有很多方法(在控制器上)只使用一些抽象方法來 CRUD(在模型上),我是在創建一個胖控制器而不是模型嗎?或者他們說,胖模型,在返回什么而不是輸入什么?這是我從未理解的事情 =) 感謝任何評論!非常感謝
Q: On MVC paradigm, its said Fat models, skinny controllers. I'm here thinking, If I have lots of methods (on controller) that uses just a few abstract methods to CRUD (on model), am I creating a fat controller instead of a model ? Or they say, fat model, refearing in what is returned and not typed ? that's something I've never understood =) Any comments are appreciated! Thanks a lot
OBS1:我不是在做模型的事情,在控制器中,我只有控制模型的方法
OBS1: I'm not doing whats ment by the model, in the controller, I just have methods that control whats going to the model
OBS2:假設checkIfEmailExists()",有john@hotmail.com"作為參數.此方法將從查詢此參數是否存在于表中的模型方法中獲取返回值,返回布爾值.如果是0,checkIFemailExists()"會調用一個不同的模型方法,這個,他只是另一個抽象方法,執行更新操作.
OBS2: let's say "checkIfEmailExists()", has "john@hotmail.com", as a parameters. This method will, get the return from the model method that querys if this param exist in table, return boolean. If is 0, "checkIFemailExists()" will call a diferent model method, this one, he's just another abstract method, that performs Update operation.
OBS3:checkIfEmailExists()",不只是一個控制器嗎?他實際上并沒有執行任何 CRUD,他只是在比較值等.這讓我感到困惑,因為在我看來,這是一個控制器:S
OBS3: The "checkIfEmailExists()", isnt just a controller ? He's not actually performing any CRUD, he's just comparing values etc. That's whats confusing me, because in my head this is a controller :S
注意:我想這不是最好的例子,因為說檢查是否存在某些東西",聽起來像是查詢我的表操作
Notes: I guess this is not the best example, since saying "check if something exists",sounds like a query my table operation
Q2:還有一個問題,所以,假設我有一個視圖表單,從那里發送電子郵件地址參數.你是說視圖直接進入模型?
Q2:just one more question, so, let's say I've got a view form, from where that email address parameter is sent from. Are you saying the view goes directly to the model ?
Q3:控制器不應該在它們之間起作用嗎?這就是范式
Q3:Shouldn't the controller act between them ? thats the paradigm
最后說明:討論結束,說我錯了,希望沒問題(我正在學習).但是,那么,Q2 和 Q3 的正確答案是什么?
FINAL NOTE: The discussion ended, saying that I'm wrong, wish is ok (i'm learning). But, so, whats the right answers for Q2 and Q3 ?
感謝您的關注
推薦答案
您的應用程序是 M.它應該能夠獨立于 V 和 C.V 和 C 構成了您的應用程序的用戶界面.無論是 Web 界面還是命令行界面,對于應用程序的核心業務邏輯的運行來說都無關緊要.您希望模型包含業務邏輯.
Your application is the M. It should be able to stand independent from V and C. V and C form the User Interface to your application. Whether this is a web interface or a command line interface shouldn't matter for the core business logic of your application to run. You want the model to be fat with business logic.
如果你有一個胖控制器,例如充滿了業務邏輯,你沒有堅持MVC的目的.控制器的唯一職責是處理 UI 請求并將其委托給模型.這就是為什么它應該是瘦的.它應該只包含它負責的工作所必需的代碼.
If you have a fat controller instead, e.g. full with business logic, you are not adhering to the purpose of MVC. A controller's sole responsibility is handling and delegating UI requests to the Model. That's why it should be skinny. It should only contain code necessary for what it's responsible for.
public function fooAction()
{
if(isset($_POST['bar'])) {
$bar = Sanitizer::sanitize($_POST['bar']);
$rows = $this->database->query('SELECT * from table');
try {
foreach($rows as $row) {
$row->foo = $bar;
$row->save();
}
} catch (Exception $e) {
$this->render('errorPage');
exit;
}
$this->render('successPage');
} else {
$this->render('fooPage');
}
}
什么時候該
public function fooAction()
{
if(isset($_POST['bar'])) {
$success = $this->tableGateway->updateFoo($_GET['bar']);
$page = $success ? 'successPage' : 'errorPage';
$this->render($page);
} else {
$this->render('fooPage');
}
}
因為這就是控制器需要知道的全部內容.它不應該更新行.它應該只是告訴模型有人要求進行此更改.更新是管理行的類的責任.此外,控制器不一定要清理該值.
because that's all the controller needs to know. It should not update the rows. It should just tell the model that someone requested this change. Updating is the responsibility of the class managing the rows. Also, the controller does not necessarily have to sanitize the value.
至于 Q2 和 Q3,請看我對 我可以從視圖中調用模型嗎.
As for Q2 and Q3, please see my answer to Can I call a Model from the View.
這篇關于理解MVC:“胖"是什么概念?在模特身上,“瘦"在控制器上?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!