Yii框架官方指南系列15—基礎知識:最佳MVC實踐

黄舟
發布: 2023-03-05 17:50:02
原創
939 人瀏覽過



儘管模型——視圖——控制器 (MVC) 被幾乎每一個Web開發人員所熟知,但是在實際應用開發中如何合理使用MVC依然困擾著許多人。 MC背後的核心思想是程式碼的可重用性以及邏輯與視圖的分離。 在本節中, 我們將會講述在使用Yii框架的過程中如何更好的使用MVC開發應用。

為了更好進行解釋闡述,我們假設Web應用包含瞭如下的一些子應用:

  • 前端: 面向終端用戶的公共網站介面;

  • 後端:提供管理整個網站應用程式的管理功能,通常只有管理員才能存取使用;

  • 控制台:可在終端視窗運行的包含控制台指令的應用;

  • Web API: 提供介面供第三方與本應用程式互動。

這些子應用可能以模組的形式實現,也可能是和其他子應用共享代碼的Yii應用。

1. 模型

Models represent the underlying data structure of a Web application. Models are often shared among different sub-applications of a Web application. For example, a in Forminnomo​​m the min c​​bek the mbele, a back end of an application; a News model may be used by the console commands, Web APIs, and the front/back end of an application. Therefore, ouldels產品🜎sh;

should contain business logic (e.g. validation rules) to ensure the represented data fulfills the design requirement;
  • may contain code for manipulating ​​p. For Searchex. may contain a
  • search
  •  method to implement the actual search.

  • Sometimes, following the last rule above may make a model very fat, containing too much code in a single may make a model . contains serves different purposes. For example, a 

    News model may contain a method named getLatestNews which is only used by the front endtestNews which is only used by the front end; News used by the back end. This may be fine for an application of small to medium size. For large applications, the following strategy may be used to make models more maintainable:

( e.g. front end, back end);In each sub-application, define a News model by extending from 

NewsBaseBase
    . all . .
  • So, if we were to employ this strategy in our above example, we would add a 

    News
  •  model in the front end applecation that contains on詢問/ tests News
  •  model背

    $ _POST, or other similar variables that are directly tied to the end-user request. Remember that a model may be used by a totally different sub-application (e.g. unit tested by a totally different sub-application (e.g. repunit test, Web API) that may sable, s​​Uentables s, Web . These variables pertaining to the user request should be handled by the Controller.should avoid embedding HTML or other presentational code. Because presentational code embedding HTML or other presentational code. Because presentational code ies according to ies presentational 筆. news in completely different formats), it is better taken care of by views.

2. 視圖Views are responsible for presenting models in the formats that general sooo, generals that thematire ly contain presentational code, such as HTML, and simple PHP code to traverse, format and render data;should avoid containing code that performs explicit DB queries. Such code is boid bone boids b片$_GET

$_POST

, or other similar variables that represent the end user request. This is the controller's job. The view should be focused on the display and , but not attempting to access request variables or the database directly.

  • 可以直接存取控制器和模型的屬性和方法。但是,這應該僅用於演示目的。

  • 視圖可以以不同的方式重複使用:

    • 版面:可以將常見的簡報區域(例如頁首、頁尾)放在版面檢視中。

    • 部分視圖:使用部分視圖(未透過佈局修飾的視圖)來重複使用示範程式碼片段。例如,我們使用_form.php 部分視圖來渲染模型建立和更新頁面中使用的模型輸入表單。

    • 小部件:如果需要大量邏輯來呈現部分視圖,則部分視圖視圖可以變成一個小部件,其類別文件是包含此邏輯的最佳位置。對於產生大量 HTML 標記的小部件,最好使用特定於小部件的視圖檔案來包含標記。

    • 幫助類:在視圖中,我們經常需要一些程式碼片段來執行一些小任務,例如格式化資料或產生 HTML 標籤。更好的方法是將所有這些程式碼片段放在視圖幫助器類別中,而不是直接將此程式碼放入視圖檔案中。然後,只需在視圖文件中使用輔助類別。 Yii 提供了這種方法的一個例子。 Yii 有一個強大的 CHtml 幫助器類,可以產生常用的 HTML 程式碼。幫助程式類別可以放入可自動載入的目錄中,以便無需明確包含類別即可使用它們。

    3. 控制器

    控制器是將模型、視圖和其他組件綁定在一起形成可運行應用程式的黏合劑。控制器負責直接處理最終使用者的請求。因此,控制器

    • 可以存取$_GET$_POST和其他代表使用者要求的PHP變數;

    • 可以建立模型實例並管理其生命週期。例如,在典型的模型更新動作中,控制器可能首先建立模型實例;然後使用來自$_POST的使用者輸入填充模型;成功儲存模型後,控制器可以將使用者瀏覽器重定向到模型詳細資訊頁面。請注意,保存模型的實際實作應該位於模型中而不是控制器中。

    • 應避免包含嵌入的 SQL 語句,這些語句最好保留在模型中。

    • 應避免包含任何 HTML 或任何其他內容演示性標記。這最好保留在視圖中。

    在設計良好的 MVC 應用程式中,控制器通常非常薄,可能只包含幾十行程式碼;而模型則非常龐大,包含負責表示和操作資料的大部分程式碼。這是因為模型所表示的資料結構和業務邏輯通常是針對特定應用程式的,需要進行大量自訂才能滿足特定的應用程式需求;而控制器邏輯通常在應用程式中遵循類似的模式,因此很可能透過底層框架或基底類別進行簡化。

    以上就是Yii框架官方指南系列15——基礎知識:最佳MVC實踐的內容,更多相關內容請關注PHP中文網(www.php.cn)!

    來源:php.cn
    本網站聲明
    本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
    熱門教學
    更多>
    最新下載
    更多>
    網站特效
    網站源碼
    網站素材
    前端模板
    關於我們 免責聲明 Sitemap
    PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!