Joomla-book/1.2-Joomla! 中關(guān)于MVC的解釋

Joomla! 中關(guān)于MVC的解釋

Introduction to MVC

MVC is a software design pattern that can be used to organize code in such a way that the business logic and data presentation are seperated. The premise behind this approach is that if the business logic is grouped into one section, then the interface and user interaction that surrounds the data can be revised and customized without having to reprogram the business logic. MVC was originally developed to map the traditional input, processing, output roles into a logical GUI architecture.

Model

The model is the part of the component that encapsulates the application's data. It will often provide routines to manage and manipulate this data in a meaningful way in addition to routines that retrieve the data from the model. In general, the underlying data access technique should be encapsulated in the model. In this way, if an application is to be moved from a system that utilizes a flat file to store its information to a system that uses a database, the model is the only element that needs to be changed, not the view or the controller.

View

The view is the part of the component that is used to render the data from the model in a manner that is suitable for interaction. For a web-based application, the view would generally be an HTML page that is returned to the user. The view pulls data from the model (which is passed to it from the controller) and feeds the data into a template which is populated and presented to the user. The view does not cause the data to be modified in any way, it only displays the data received from the model.

Controller

The controller is responsible for responding to user actions. In the case of a web application, a user action is generally a page request. The controller will determine what request is being made by the user and respond appropriately by triggering the model to manipulate the data appropriately and passing the model into the view. The controller does not display the data in the model, it only triggers methods in the model which modify the data, and then pass the model into the view which displays the data.

Joomla! Component Framework Explained

在 Joomla! Framework 中,models 負(fù)責(zé)管理數(shù)據(jù)。每一個(gè) model 中一個(gè)必須寫(xiě)的 function 就是 get function,它用來(lái)返回?cái)?shù)據(jù)給請(qǐng)求者。舉個(gè)例子,這個(gè)請(qǐng)求者就是下面的 HelloWorldViewHelloWorld 視圖(view)。默認(rèn)情況下,命名為 HelloWorldModelHelloWorld 的、位于 site/models/helloworld.php的 model 文件是這個(gè)視圖的主要相關(guān) model。
所以,這里我們有一些命名規(guī)范要記住,這些規(guī)范是魔法生效的前提,便于我們理解類(lèi)與類(lèi)之間是如何調(diào)用的。
躺在 site/views/helloworld/view.html.php 中的 HelloWorldViewHelloWorld 類(lèi)會(huì)利用到躺在site/models/helloworld.php中的 HelloWorldModelHelloWorld類(lèi)。

再舉個(gè)例子,我們想使用一個(gè)叫fluffy的視圖,你必須有:

  1. 躺在 site/views/fluffy/view.html.php中的HelloWorldViewFluffy類(lèi)
  2. 躺在 site/models/fluffy.php中的HelloWorldModelFluffy類(lèi)
  3. Note: the actual screen of the view: site/views/fluffy/tmpl/default.php is required as well to make this example work.

少了上述任一規(guī)范,都會(huì)導(dǎo)致錯(cuò)誤出現(xiàn)空白頁(yè)。

如何訪問(wèn)Joomla!組件

訪問(wèn)首頁(yè)

用戶(hù)訪問(wèn)前端: www.yoursitename.com/index.php,可以通過(guò)nginx配置隱藏index.php;

管理員訪問(wèn)后臺(tái):www.yoursitename.com/administrator/index.php。

訪問(wèn)某組件

用戶(hù)訪問(wèn):www.yoursitename.com/index.php?option=com_<component_name>

管理員訪問(wèn)后臺(tái):www.yoursitename.com/administrator/index.php?option=com_<component_name>。

舉個(gè) HelloWorld 組件的例子:

www.myjoomla.com/index.php?option=com_helloworld

基本的MVC目錄結(jié)構(gòu)

Components 都放在一個(gè)指定的目錄當(dāng)中,即 components文件夾,如: /Users/Workspace/Joomla/components/
那么 HelloWorld 組件就是放在 /Users/Workspace/Joomla/components/com_helloworld

一個(gè)基本的組件包括以下文件:

每一個(gè)文件夾下都會(huì)放一個(gè)空白內(nèi)容的 index.html,這是出于安全考慮,主要是防止索引文件夾。

  1. 一個(gè)出于安全考慮的空白內(nèi)容文件:index.html
  2. 一個(gè)代表 controller 自己的 php 文件:controller.php
  3. 一個(gè)加載 controller 類(lèi)的 php 文件:<component_name>.php
  4. 一個(gè)代表 model 自己的 php 文件 models/<component_name>.php
  5. 另一個(gè)空白文件:models/index.html
  6. 一個(gè)包含默認(rèn)視圖的 php 文件:views/<component_name>/tmpl/default.php
  7. A xml file for adding a menu item type:views/<component_name>/tmpl/default.xml
  8. 各個(gè)文件夾下的 index.html
  9. 一個(gè)用來(lái)顯示視圖的 php 文件:views/<component_name>/view.html.php

以上只是基本的文件,你可以根據(jù)需要任意添加,具體看看 joomla! 自帶的那些 components 就知道啦。

com_search 舉例:

com_search
├── controller.php
├── models
│   └── search.php
├── router.php
├── search.php
└── views
    └── search
        ├── metadata.xml
        ├── tmpl
        │   ├── default.php
        │   ├── default.xml
        │   ├── default_error.php
        │   ├── default_form.php
        │   └── default_results.php
        ├── view.html.php
        └── view.opensearch.php

JEXEC 常量

每一個(gè) php 文件開(kāi)頭都須放置一串這樣的代碼:

<?php
defined('_JEXEC') or die;

防止直接訪問(wèn) php 文件時(shí)可能出現(xiàn)的報(bào)錯(cuò)信息。

首發(fā)Github

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 不想說(shuō)告別 相思成蹉跎 我愿擁抱這孤獨(dú) 駐守在黑夜 不想說(shuō)告別 相思是首歌 我愿背起木吉他 慢慢來(lái)訴說(shuō) 歌聲迷路在...
    雷聲狂閱讀 358評(píng)論 0 3
  • 1 自私,囂張,任性,大多數(shù)人是這么評(píng)論萱萱的。 軍訓(xùn)時(shí)襖熱難當(dāng),我們簡(jiǎn)直是悶在蒸籠里的黑土豆??恐俨±舆^(guò)軍訓(xùn)...
    愛(ài)恨的潮汐閱讀 976評(píng)論 0 0

友情鏈接更多精彩內(nèi)容