Joomla! 中关于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 负责管理数据。每一个 model 中一个必须写的 function 就是 get function,它用来返回数据给请求者。举个例子,这个请求者就是下面的 HelloWorldViewHelloWorld
视图(view)。默认情况下,命名为 HelloWorldModelHelloWorld
的、位于 site/models/helloworld.php
的 model 文件是这个视图的主要相关 model。
所以,这里我们有一些命名规范要记住,这些规范是魔法生效的前提,便于我们理解类与类之间是如何调用的。
躺在 site/views/helloworld/view.html.php
中的 HelloWorldViewHelloWorld
类会利用到躺在site/models/helloworld.php
中的 HelloWorldModelHelloWorld
类。
再举个例子,我们想使用一个叫fluffy
的视图,你必须有:
- 躺在
site/views/fluffy/view.html.php
中的HelloWorldViewFluffy
类 - 躺在
site/models/fluffy.php
中的HelloWorldModelFluffy
类 - Note: the actual screen of the view:
site/views/fluffy/tmpl/default.php
is required as well to make this example work.
少了上述任一规范,都会导致错误出现空白页。
如何访问Joomla!组件
访问首页
用户访问前端: www.yoursitename.com/index.php
,可以通过nginx配置隐藏index.php;
管理员访问后台:www.yoursitename.com/administrator/index.php
。
访问某组件
用户访问:www.yoursitename.com/index.php?option=com_<component_name>
管理员访问后台:www.yoursitename.com/administrator/index.php?option=com_<component_name>
。
举个 HelloWorld 组件的例子:
www.myjoomla.com/index.php?option=com_helloworld
基本的MVC目录结构
Components 都放在一个指定的目录当中,即 components
文件夹,如: /Users/Workspace/Joomla/components/
。
那么 HelloWorld 组件就是放在 /Users/Workspace/Joomla/components/com_helloworld
。
一个基本的组件包括以下文件:
每一个文件夹下都会放一个空白内容的
index.html
,这是出于安全考虑,主要是防止索引文件夹。
- 一个出于安全考虑的空白内容文件:
index.html
- 一个代表 controller 自己的 php 文件:
controller.php
- 一个加载 controller 类的 php 文件:
<component_name>.php
- 一个代表 model 自己的 php 文件
models/<component_name>.php
- 另一个空白文件:
models/index.html
- 一个包含默认视图的 php 文件:
views/<component_name>/tmpl/default.php
- A xml file for adding a menu item type:
views/<component_name>/tmpl/default.xml
- 各个文件夹下的
index.html
- 一个用来显示视图的 php 文件:
views/<component_name>/view.html.php
以上只是基本的文件,你可以根据需要任意添加,具体看看 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 常量
每一个 php 文件开头都须放置一串这样的代码:
<?php
defined('_JEXEC') or die;
防止直接访问 php 文件时可能出现的报错信息。