在 Joomla!2.5 框架中, 第三方组件通常分为三部分,即MVC:
· 数据模型Models(M) 管理数据。
· 控制器Controllers(C)处理任务(tasks),设置和获取数据模型(models)状态并提交视图(views)进行显示。
· 视图Views(V) 根据类型(error, feed, html, json, raw, xml)和选定的“Layout”显示内容
设置控制器
在Joomla内核中内置有一个类管理控制器,即: Jcontroller,这个类必须先扩展。 在文件 site/helloworld.php (Hello World 组件的入口文件), 输入以下内容:
<?php
// 如果无直接数据
defined('_JEXEC') or die('Restricted access');
// 导入controller库
jimport('joomla.application.component.controller');
// 或许一个以HelloWorld为前缀的控制器
$controller = JController::getInstance('HelloWorld');
// 处理请求的任务(task)
$controller->execute(JRequest::getCmd('task'));
// 根据控制器设置进行重定向
$controller->redirect();
getInstance 是 Jcontroller类的静态方法,用于生成一个controller实例,在该组件中,它将生成一个叫做 “HelloWorldController”的类。 Joomla会根据这个名字在controller.php(默认的)这个控制器汇总寻找这个类的 声明。
下面我们就创建 controller.php 这个文件并建立HelloWorldController 这个类。
site/controller.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
* Hello World Component Controller
*/
class HelloWorldController extends JController
{
}
在请求(request)变量中如果没有任务,就执行默认任务——显示。JController 中有这个任务的处理办法,在该组件中将显示一个HelloWorld视图(view)。
设置视图
当 JController 想要显示视图的时候, 会在这个文件夹中寻找 component/com_[组件名]/views/[视图名]/。
默认视图的名字和组件名相同。 本示例中为 component/com_helloworld/views/helloworld/。
包含代码的视图文件这样命名view.[视图模式].php。 “视图模式”通常是html 模式,所以视图文件通常为view.html.php,下面我们来创建这个文件,注意文件路径。
site/views/helloworld/view.html.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// import Joomla view library
jimport('joomla.application.component.view');
/**
* HTML View class for the HelloWorld Component
*/
class HelloWorldViewHelloWorld extends JView
{
// Overwriting JView display method
function display($tpl = null)
{
// Assign data to the view
$this->msg = 'Hello World';
// Display the view
parent::display($tpl);
}
}
JController 类的display任务调用Jview类的display方法。本例中调用tmpl/default.php 这个模板文件,这个文件将显示默认视图的内容。
site/views/helloworld/tmpl/default.php
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
?>
<h1><?php echo $this->msg; ?></h1>
这个模板文件被 JView 自动包含。所以在这里 $this 代表 HelloWorldViewHelloWorld 这个类。
组件打包
现在代码目录如下(每个文件夹下还是要有一个index.html文件,内容都一样):
· helloworld.xml
· site/index.html
· site/helloworld.php
· site/controller.php
· site/views/index.html
· site/views/helloworld/index.html
· site/views/helloworld/view.html.php
· site/views/helloworld/tmpl/index.html
· site/views/helloworld/tmpl/default.php
· admin/index.html
· admin/helloworld.php
· admin/sql/index.html
· admin/sql/updates/index.html
· admin/sql/updates/mysql/index.html
· admin/sql/updates/mysql/0.0.1.sql
修改helloworld.xml如下,然后打包安装一下试试吧。
<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">
<name>Hello World!</name>
<!-- The following elements are optional and free of formatting constraints -->
<creationDate>November 2009</creationDate>
<author>John Doe</author>
<authorEmail>这个 E-mail 受反垃圾邮件程序保护,您需要启用 JavaScript 才能查看。</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>0.0.2</version>
<!-- The description is optional and defaults to the name -->
<description>Description of the Hello World component ...</description>
<update> <!-- Runs on update; New in 2.5 -->
<schemas>
<schemapath type="mysql">sql/updates/mysql</schemapath>
</schemas>
</update>
<!-- 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>index.html</filename>
<filename>helloworld.php</filename>
<filename>controller.php</filename>
<folder>views</folder>
</files>
<administration>
<!-- Administration Menu Section -->
<menu>Hello World!</menu>
<!-- Administration 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 /admin/ in the package -->
<files folder="admin">
<!-- Admin Main File Copy Section -->
<filename>index.html</filename>
<filename>helloworld.php</filename>
<!-- SQL files section -->
<folder>sql</folder>
</files>
</administration>
</extension>
结果:浏览[yoursite]/index.php?option=com_helloworld,你将会看到view.html.php 中变量$this->msg 所存放的内容——Hello World,当然这个内容你可以自行修改。
评论 (0)