Joomla文档中文翻译 - MVC组件开发:模型 - 使用模型
在昨天的文档中,我们了解了怎样为Joomla的组件创建模型,今天我们来看看怎样使模型配合组件的其他部分来共同工作。
正如我们在“Joomla文档中文翻译 - MVC组件开发:基础 - 什么是MVC”一文中了解到的,Joomla MVC组件的控制器会自动加载组件中的模型,在获取了正确的数据后,模型将数据内容推送给同名路径中的视图文件(view),通过模板进行前台页面呈现。
现在来看我们的hello组件。我们在之前的文档中已经学习了怎样为组件创建视图及模板,模型在获取了数据之后,会自动选择名为hello的view路径中的视图文件进行数据推送。接下来我们要在之前创建的视图文件做一些修改,通过JView::getModel()方法,使视图可以获取到模型传递过来的数据内容。打开之前的视图文件site/views/hello/view.html.php,找到下面这行代码:
$greeting = "Hello World!";
将其替换为:
$model = &$this->getModel();
$greeting = $model->getGreeting();
现在,完整的view.html.php文件的代码是这样的:
<?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);
}
}
搞定。到目前,我们的hello组件已经比较完整了,包括入口点、控制器、模型、视图及模板文件;由于我们之前创建的hello.xml的时候还没有开始做模型,所以现在需要将模型文件添加到hello.xml中。关于xml文件的创建,请参考“Joomla文档中文翻译 - MVC组件开发:基础 - 创建组件(5) XML文件”一文;我们将下面这行代码添加到<files>列表中:
<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> This e-mail address is being protected from spambots. You need JavaScript enabled to view it </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>
这样,我们的hello组件已经基本成型了,但距离实际使用还有一段距离,例如数据库的使用及后台管理界面等。明天开始我们进入第三部分的学习。
Joomla文档中文翻译 - MVC组件开发:基础Joomla文档中文翻译 - MVC组件开发:创建模型及相关扩展- Joomla文档中文翻译 - MVC组件开发:使用数据库
- Joomla文档中文翻译 - MVC组件开发:创建后台管理界面
- Joomla文档中文翻译 - MVC组件开发:后台基本框架
- Joomla文档中文翻译 - MVC组件开发:添加后台行为
译者:Viiiix7210
如需转载,请注明官方英文文档及本人译文的出处,谢谢。查看官方英文文档: Developing a Model-View-Controller Component - Part 2
| < Prev | Next > |
|---|
