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

MVC:這是初始化的正確順序嗎?調(diào)用 MVC 層

MVC : Is this the correct sequence to initialize amp; invoke MVC layers(MVC:這是初始化的正確順序嗎?調(diào)用 MVC 層)
本文介紹了MVC:這是初始化的正確順序嗎?調(diào)用 MVC 層的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試學(xué)習(xí)基于 php MVC 的網(wǎng)頁開發(fā).起初我真的認(rèn)為它只是將項(xiàng)目解耦為 3 個(gè)類模型、視圖、控制器.但是在幫助之前的帖子評論中,我意識到這些不是類而是相反,它們是層

I am trying to learn php MVC based webpage development.At first i literally thougt that it's just decoupling the project into 3 classes Model, View, Controller.But with help SO previous post comments, i realized that these are Not class but instead they are layers

// don't be confused my class/OOP style, it is just for conceptual purpose
Model.php Layer related code   
View.php Layer  related code   
Controller.php Layer related code   

User: index.php
//initiating model layer related things
$m = new Model;
// initiating Controller layer related things
$v = new Controller($m);
// initiating view layer related things
$c = new View($m, $c);

然而,互聯(lián)網(wǎng)上有許多 MVC 示例有時(shí)令人困惑和沖突.例如,有些人建議:控制器具有模型和模型的訪問權(quán);視圖,其他建議視圖可以訪問兩者.所以請任何人檢查我的代碼序列以確保它正確遵循 MVC 模式.

However, there are many MVC example over internet which are confusing and conflicting sometime.For example some suggest: controller have the access of both model & view, where other suggest view have the access of both.So please anyone check my code sequence to ensure that it does follow the MVC pattern correctly.

推薦答案

在桌面應(yīng)用程序的原始 MVC 模式(由 Trygve Reenskaug 于 1979 年提出)中,控制器更新模型,模型通知視圖有關(guān)更改,然后視圖從中提取數(shù)據(jù).此外,M、V &C 組件被認(rèn)為與窗口中的單個(gè)控件(按鈕、文本框、復(fù)選框等)相關(guān).如果屏幕上有 15 個(gè)控件,那么每個(gè)控件都附加"了一個(gè) MVC.

In the original MVC pattern for the desktop apps (presented by Trygve Reenskaug in 1979), the controller updates the model, the model notifies the view about the changes, and then the view pulls its data from it. Also, the M, V & C components were thought as related to a single control in a window (a button, a textbox, a checkbox, etc). If you had 15 controls on screen, then each of them had an MVC "attached" to it.

在 Web 應(yīng)用程序中,不存在通知步驟(從模型到視圖).但其余的 M、V & 之間的關(guān)系C 組件得到維護(hù).因此,在 Web MVC 中,控制器更新模型,視圖從中提取數(shù)據(jù).此外,所有三個(gè)組件都與網(wǎng)頁相關(guān),而不是與網(wǎng)頁上的單個(gè)控件相關(guān).

In the web applications, the notification step (from model to view) is not present. But the rest of the relations between the M, V & C components is maintained. So, in a web MVC, the controller updates the model and the view pulls its data from it. Also, all three components are related to a web page, not to a single control on it.

控制器和視圖都不應(yīng)使用域?qū)ο?/em>,例如實(shí)體(作為域模型的一部分)直接.它們僅通過應(yīng)用服務(wù)(也稱為用例)與域模型(單獨(dú))通信.這些服務(wù)可以將(多個(gè))任務(wù)委托給(多個(gè))域服務(wù).進(jìn)而利用域?qū)ο?/em>(實(shí)體).如果需要外部組件和/或服務(wù)(例如持久層中的組件和/或服務(wù),例如存儲庫、數(shù)據(jù)映射器),那么域模型和它們之間的連接是通過使用相應(yīng)的接口.這些也是領(lǐng)域?qū)拥囊徊糠?

Both, controller and view, should never use domain objects, e.g. entities (as part of the domain model) directly. They communicate (separately) with the domain model only through application services (also named use cases). These services may delegate (multiple) tasks to (multiple) domain services. Which, in turn make use of the domain objects (entities). If external components and/or services are needed (like the ones in the persistence layer, e.g. repositories, datamappers, for example), then the connection between the domain model and them is made through the use of corresponding interfaces. These also are part of the domain layer.

視圖不應(yīng)該訪問控制器.為什么要這樣做?控制器解釋用戶發(fā)送的請求(通過 HTTP 協(xié)議,在 Web 應(yīng)用程序中)并將所有域相關(guān)的工作推遲到域中的服務(wù)層,將請求數(shù)據(jù)傳遞給它.所以控制器只影響域?qū)拥淖兓?/strong>.另一方面,視圖僅從域?qū)诱埱髷?shù)據(jù).它也通過服務(wù)來做到這一點(diǎn).所以視圖從域中讀取.因此,在您的代碼中,視圖應(yīng)該只接收 $m 作為參數(shù).

The view should never access the controller. Why should it, at all? The controller interprets the request sent by the user (through the HTTP protocol, in web apps) and defers all the domain related work to the service layer in the domain, passing it the request data. So the controller only effects changes in the domain layer. On the other side, the view requests data only from the domain layer. It does this through services as well. So the view reads from the domain. Therefore, in your code, the view should receive only the $m as argument.

關(guān)于直接從域?qū)永?shù)據(jù)的視圖有一個(gè)重要方面:如果視圖希望簡單地直接呈現(xiàn)從域接收到的數(shù)據(jù),那么這意味著域?qū)⒇?fù)責(zé)準(zhǔn)備(例如格式化)視圖的數(shù)據(jù).但這意味著模型應(yīng)該知道準(zhǔn)備的數(shù)據(jù)采用哪種格式.例如.域模型必須了解位于其邊界之外的組件.這不會很好.因此,視圖的作用是接收未準(zhǔn)備好的數(shù)據(jù),為網(wǎng)頁格式化并呈現(xiàn)它.總之,視圖執(zhí)行呈現(xiàn)邏輯.

There is an important aspect regarding the view pulling data directly from the domain layer: If the view would expect to simply, directly present the data received from the domain, then this would mean that the domain would be responsible with the preparation (e.g. formatting) of the data for the view. But that would imply, that the model should know, in which format is to be the data prepared. E.g. the domain model would have to have knowledge about components residing outside its boundaries. This would not be good. So the role of the view is to receive unprepared data, to format it for the web page, and to present it. In a word, the view executes the presentation logic.

下面的鏈接中是 Robert C. Martin 的精彩演講,介紹了如何在應(yīng)用程序中涉及的組件之間分離關(guān)注點(diǎn),從而形成良好的應(yīng)用程序架構(gòu).將上述想法與演示文稿中提出的想法進(jìn)行比較,您會發(fā)現(xiàn)它們之間存在兩個(gè)不同之處.

In a link bellow is a great presentation by Robert C. Martin about how the concerns should be separated between the components involved in an application, resulting in a good application architecture. Making a parallel between the above ideas and the ones presented in the presentation, you will discover, that there are two differences between them.

第一個(gè)是,在視頻中,展示層由展示器、視圖模型和視圖組成.而在上述 Web MVC 中,整個(gè)呈現(xiàn)邏輯僅由視圖執(zhí)行 - 這不太好.

The first one is, that, in the video, the presentation layer consists of a presenter, a view model and a view. Whereas in the web MVC described above, the whole presentation logic is performed only by the view - which is not so good.

第二種是,在視頻中,表示層要處理和顯示的數(shù)據(jù)從控制器通過域?qū)?strong>推送到表示層.現(xiàn)在試著想象一下,演示者依賴于交互者(實(shí)際上是一個(gè)應(yīng)用服務(wù),一個(gè)用例).例如.從呈現(xiàn)者指向領(lǐng)域?qū)拥募^描述了一種依賴關(guān)系,而不是一種繼承關(guān)系.然后演示者的作用是將信息拉出域?qū)?與上面介紹的 Web MVC 的類比是顯而易見的.

The second one is, that, in the video, the data to be processed and displayed by the presentation layer is pushed from the controller through the domain layer to the presentation layer. Now try to imagine yourself, that that presenter depends on an interactor (which, actually is an application service, a use case). E.g. that the arrow pointing from the presenter to the domain layer describes a dependency relation, instead of an inheritance one. Then the role of the presenter will be to pull information out of the domain layer. The analogy with the web MVC presented above is then obvious.

資源:

主題演講:失落歲月的建筑 - 給出的演示文稿由 Robert Martin 提供,根據(jù) Creative Commons Attribution ShareAlike 3.0.

Keynote: Architecture the Lost Years - Presentation given by Robert Martin, licensed under a Creative Commons Attribution ShareAlike 3.0.

示例 1(在 index.php 中):顯示數(shù)據(jù)

用戶在瀏覽器的地址欄中引入 URL http://localhost/questions/12345 并期望一個(gè)帶有問題和多個(gè)評論的 html 頁面.因此 HTTP 方法是GET".所以不需要控制器,只需要一個(gè)視圖(用于從域模型中讀取和顯示數(shù)據(jù)).

The user introduces the URL http://localhost/questions/12345 in the address bar of the browser and expects a html page with a question and multiple comments for it. Therefore the HTTP method is "GET". So no controller is needed, but only a view (for reading and displaying data from the domain model).

<?php

namespace TestMvcExample;

use LibHttpMessageResponse;
use LibRouterRouteCollection;
use LibHttpMessageServerRequest;
use LibHttpMessageEmitterResponseEmitter;

/*
 * -----------------------------------------
 * Before dispatching by a front-controller.
 * -----------------------------------------
 */
$routes = new RouteCollection();
$routes->add('GET', '/questions/{id:d+}', [
    'view' => [QuestionsView::class, 'index'],
]);

$request = new ServerRequest('GET', '/questions/12345' /* , other args */);
$response = new Response(/* args */);

/*
 * ------------------------------------------------------------------
 * Dispatched by a front-controller.
 * Route params are saved into the attributes list of server request.
 * ------------------------------------------------------------------
 */
$view = new QuestionsView(new Template(), new QuestionsService(), new CommentsService());

$response = $view->index($response, $request->getAttribute('id'));

/*
 * ----------------------------------------
 * After dispatching by a front-controller.
 * ----------------------------------------
 */
$responseEmitter = new ResponseEmitter();
$responseEmitter->emitResponse($response);

示例 2(在 index.php 中):更新和顯示數(shù)據(jù)

在 URL http://localhost/questions/edit/12345 上,用戶編輯帶有屬性 action="/questions/update" 的表單中的問題并點(diǎn)擊提交按鈕更新".因此 HTTP 方法是POST".因此需要一個(gè)控制器(用于更新模型層)和一個(gè)視圖(用于從模型層讀取和顯示數(shù)據(jù)).

At URL http://localhost/questions/edit/12345 the user edits the question in a form with attribute action="/questions/update" and clicks the submit button "Update". Therefore the HTTP method is "POST". So both a controller (for updating the model layer) and a view (for reading and displaying data from the model layer) are needed.

您可以更改為其他要求,而不是僅使用視圖.例如,要使用演示者和視圖,兩者共享一個(gè)視圖模型.

Instead of only using a view, one can change to other requirements. For example, to use a presenter and a view, both sharing a view-model.

<?php

namespace TestMvcExample;

use LibHttpMessageResponse;
use LibRouterRouteCollection;
use LibHttpMessageServerRequest;
use LibHttpMessageEmitterResponseEmitter;

/*
 * -----------------------------------------
 * Before dispatching by a front-controller.
 * -----------------------------------------
 */
$routes = new RouteCollection();
$routes->add('POST', '/questions/update', [
    'controller' => [QuestionsController::class, 'updateQuestion'],
    'view' => [QuestionsView::class, 'getQuestionUpdated'],
]);

$request = new ServerRequest('POST', '/questions/update' /* , other args */);
$response = new Response(/* args */);

/*
 * ----------------------------------
 * Dispatched by a front-controller.
 * ----------------------------------
 */
$questionsService = new QuestionsService();

$controller = new QuestionsController($questionsService);
$controller->updateQuestion($request);

$view = new QuestionsView(new Template(), $questionsService);

$response = $view->getQuestionUpdated($response, $request);

/*
 * ----------------------------------------
 * After dispatching by a front-controller.
 * ----------------------------------------
 */
$responseEmitter = new ResponseEmitter();
$responseEmitter->emitResponse($response);

這篇關(guān)于MVC:這是初始化的正確順序嗎?調(diào)用 MVC 層的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

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 中的部分(視圖),以便我的應(yīng)用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發(fā)技術(shù)
Having a single entry point to a website. Bad? Good? Non-issue?(有一個(gè)網(wǎng)站的單一入口點(diǎn).壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務(wù)層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: www精品美女久久久tv | 一级免费看片 | 丝袜美腿一区二区三区动态图 | 亚洲一区二区三区在线视频 | 欧美一区二区三区在线视频 | 黑人巨大精品欧美一区二区免费 | 国产一区二区欧美 | 中文字幕三区 | 一区二区三区免费在线观看 | 91精品国产手机 | av国产精品 | 国产免费又色又爽又黄在线观看 | 国精久久 | 麻豆精品久久 | 日韩免费网站 | 国产精品久久久久久久久图文区 | 中午字幕在线观看 | 日本粉嫩一区二区三区视频 | 午夜一区| 九九伊人sl水蜜桃色推荐 | 亚洲一区二区网站 | 国产成人高清在线观看 | 一级片成人 | 一级黄色片免费 | 国产大学生情侣呻吟视频 | 日韩电影免费在线观看中文字幕 | 亚洲免费婷婷 | 999精彩视频 | 好姑娘影视在线观看高清 | 日韩在线 | 国产成人免费视频网站高清观看视频 | 久久久性 | 91av免费版| 性欧美精品一区二区三区在线播放 | 成人av一区二区在线观看 | 欧美国产日韩精品 | 天天操夜夜艹 | 亚洲精品国产一区 | 国产精品久久久久久久久久久久久久 | 国产精品九九 | 国产亚洲精品综合一区 |