久久久久久久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 | 综合色在线 | av在线视 | 日本一区二区不卡 | 亚州精品天堂中文字幕 | 亚洲视频网 | 欧美激情一区二区三级高清视频 | 无码日韩精品一区二区免费 | 中文字幕一二三 | 国产精品久久精品 | 四虎影院在线观看免费视频 | 欧美性生活免费 | 亚洲精品一区二区在线观看 | 国产精品福利在线 | 日韩超碰在线 | 91精品国产777在线观看 | 国产精品污www一区二区三区 | 亚洲有码转帖 | www亚洲精品 | 欧美一级免费看 | 五月激情婷婷在线 | 午夜小视频在线播放 | 三级av在线 | 亚洲高清一区二区三区 | 一区二区三区国产精品 | 精品1区2区3区4区 | 久久久久久黄 | 中文字幕av亚洲精品一部二部 | www久| 免费在线播放黄色 | 国产亚洲一区二区精品 | 国产精品久久久久久久白浊 | 久久一 | 亚州成人| 日韩三级一区 | 国产精品久久久久久久免费观看 | 久久精品一二三影院 | 激情一区二区三区 | 成人免费一级视频 |