在第一部分里用Joomla1.5的框架建立了一个简单的视图-控制器的组件,在第一部分建的组件视图
部分就是一句问候.没有完全基于MVC模式,主要是由于视图是故意仅仅是显示一组数据而不是包含它.第二部分将介绍如何将些移出视图而把它放在模型中去.在下面我们将会意识到这种设计模式强大的功能和灵活性.
建立一个模型
模型(Model)用于封装与应用程序的业务逻辑相关的数据以及对数据的处理方法。“模型”有对数据直接访问的权利,例如对数据库的访问。“模型 ”不依赖“视图”和“控制器”,也就是说,模型不关心它会被如何显示或是如何被操作。但是模型中数据的变化一般会通过一种刷新机制被公布。为了实现这种机 制,那些用于监视此模型的视图必须事先在此模型上注册,从而,视图可以了解在数据模型上发生的改变。
在joomla框架里模型的命名约定俗成类的名字和该组件的名字一起作为开始在前面(例如这个示例的组件名"hello",就是hello后面在跟着模型名,所以我们该类的名字就应该叫:HelloModleHello.
这里,我们只有一个模型的行为,就是检索返回问候.于是我们就有一个方法:getGreeting(),它将非常简单的返回字符串"Hello World".
代码如下:
<?php /**你应该会注意到开始有一行带有jimport,jimport函数是用来载入joomla的框架来满足该组件的需求.
* Hello Model for Hello World Component
*
* @package Joomla.Tutorials
* @subpackage Components
* @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
* @license GNU/GPL
*/ // No direct access defined( '_JEXEC' ) or die( 'Restricted access' ); jimport( 'joomla.application.component.model' ); /**
* Hello Model
*
* @package Joomla.Tutorials
* @subpackage Components
*/ class HelloModelHello extends JModel { /**
* Gets the greeting
* @return string The greeting to be displayed to the user
*/ function getGreeting() { return 'Hello, World!'; } }
这个特殊的声明将载入/libraries/joomla/application/component/model.php文件,'.'点是文件夹的分隔符,最后一个就是要载入的文件.所有与libraries文件夹相关的文件都被载入.这个特别的文件包含了JModel 类的定义.我们的模型的类必须要继续该类.
现在我们人模型已经建立好了,我们必须修改视图文件来获得这个问候.
使用模型
joomla的框架是这样的一个流程,控制器将会自动的载入与view视图同名的model模型,然后将数据在发送到视图,我们的视图叫"hello",我们的叫"hello"的模型将会被自动的载入然后发到视图.因此我们可以轻松的用JView::getModel()从我们的模型里返回一个引用.(如果这个模型没有按照这个约定,我们必须指定一个名字给JView::getModel());
我们之前的代码包含了这样一行:
$greeting = "Hello World!";</pre> 为了利用模型 ,我们可更改为:添加文件到安装包里: 添加一个条目到XML文件里去以让我们的新的模型文件在安装时被复制安装上去 ,这样Joomla!框架 在安装时才将会在模型的文件夹里寻找这个模型文件,像下面这样:$model =& $this->getModel(); $greeting = $model->getGreeting();完整的视图代码应该像这样: <pre> <?php /**
* @package Joomla.Tutorials
* @subpackage Components
* @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_2
* @license GNU/GPL
*/ // No direct access defined( '_JEXEC' ) or die( 'Restricted access' ); jimport( 'joomla.application.component.view'); /**
* HTML View class for the HelloWorld Component
*
* @package HelloWorld
*/ class HelloViewHello extends JView { function display($tpl = null) { $model =& $this->getModel(); $greeting = $model->getGreeting(); $this->assignRef( 'greeting', $greeting ); parent::display($tpl); } }
<filename>models/hello.php</filename>新的XML文件应应该像下面这样:
<?xml version="1.0" encoding="utf-8"?> <install type="component" version="1.5.0"> <name>Hello</name> <!-- The following elements are optional and free of formatting conttraints --> <creationDate>2007-02-22</creationDate> <author>John Doe</author> <authorEmail> 该E-mail地址已受到防止垃圾邮件机器人的保护,您必须启用浏览器的Java Script才能看到。 </authorEmail> <authorUrl>http://www.example.org</authorUrl> <copyright>Copyright Info</copyright> <license>License Info</license> <!-- The version string is recorded in the components table --> <version>1.01</version> <!-- The description is optional and defaults to the name --> <description>Description of the component ...</description> <!-- Site Main File Copy Section --> <!-- Note the folder attribute: This attribute describes the folder
to copy FROM in the package to install therefore files copied
in this section are copied from /site/ in the package --> <files folder="site"> <filename>controller.php</filename> <filename>hello.php</filename> <filename>index.html</filename> <filename>models/hello.php</filename> <filename>models/index.html</filename> <filename>views/index.html</filename> <filename>views/hello/index.html</filename> <filename>views/hello/view.html.php</filename> <filename>views/hello/tmpl/default.php</filename> <filename>views/hello/tmpl/index.html</filename> </files> <administration> <!-- Administration Menu Section --> <menu>Hello World!</menu> <!-- Administration Main File Copy Section --> <files folder="admin"> <filename>hello.php</filename> <filename>index.html</filename> </files> </administration> </install>
现在,我们就有一个基于MVC模型设计的组件了,虽然非常简单,却是强大和灵活的.
Set as favorite
Bookmark
Email This
Hits: 901
Write comment
