久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

理解MVC:“胖"是什么概念?在模特身上,“瘦

Understanding MVC: Whats the concept of quot;Fatquot; on models, quot;Skinnyquot; on controllers?(理解MVC:“胖是什么概念?在模特身上,“瘦在控制器上?)
本文介紹了理解MVC:“胖"是什么概念?在模特身上,“瘦"在控制器上?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我試圖理解模型上的胖"與控制器上的瘦"的概念,并根據我一直在討論的內容,我有以下示例(摘自 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 欧美精品在线一区二区三区 | 日日噜噜夜夜爽爽狠狠 | 久久爱综合 | 在线视频中文字幕 | 国产综合在线视频 | 久久99蜜桃综合影院免费观看 | 一级大片免费 | 天天视频一区二区三区 | 精品成人一区二区 | 精品国产欧美一区二区三区成人 | 国产资源在线观看 | 国产亚洲高清视频 | 久久久www成人免费精品 | 国产精品久久久久久久久久免费 | 日韩成人在线视频 | 人人干人人干人人 | 在线激情视频 | 欧美激情欧美激情在线五月 | 99精品视频网 | 久在线观看 | 亚洲播放 | 在线播放国产一区二区三区 | 日日日日日日bbbbb视频 | 在线成人| 国产a爽一区二区久久久 | 中文字幕第100页 | 亚洲欧美在线观看视频 | 天堂男人av | 亚洲三级在线 | 欧美lesbianxxxxhd视频社区 | 亚洲欧洲精品一区 | 国产一区二区三区免费观看视频 | 国产精品视频导航 | 国产激情一区二区三区 | 欧美成人一级 | 91久久精品国产 | 99re视频在线 | 国产人成精品一区二区三 | av大片在线| 国产男女视频 | 伊人伊成久久人综合网站 |