Joomla!-开源天空

2008-10-08
首页 专栏热点 Joomla! 应用 Joomla的模块module 开发教程(三)创建hello_world模块 下


Joomla的模块module 开发教程(三)创建hello_world模块 下

E-mail

创建 helper.php

helper.php这个文件包含 helper 类,这个类主要用来获取模块要显示的数据。我们已经说过,现在这个helper类只有一个方法 getHello(),这个方法返回‘Hello, World’

以下是 helper.php 的代码:

<?php
/**
 * Helper class for Hello World! module
 *
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @link http://dev.joomla.org/component/option,com_jd-wiki/Itemid,31/id,tutorials:modules/
 * @license        GNU/GPL, see LICENSE.php
 * mod_helloworld is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */
class modHelloWorldHelper
{
    /**
     * Retrieves the hello message
     *
     * @param array $params An object containing the module parameters
     * @access public
     */   
    function getHello( $params )
    {
        return 'Hello, World!';
    }
}

没有对定我们必须这样命名我们的helper类,但是这样命名有利于我们找到和统一。
一些复杂的模块可以在helper的方法中包含数据库请求和其他功能。

创建 tmpl/default.php

default.php file 是模板文件,他的代码如下:
<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<?php echo $hello; ?>

需要注意的一点是 模板文件和 mod_helloworld.php 是同一个变量作用域,也就是说,在mod_helloworld.php中定义的变量 $hello可以直接在模板文件中使用,而不必额外的声明或者函数调用。


创建 mod_helloworld.xml

mod_helloworld.xml主要是用来指定那些文件安装时候要拷贝以及告诉模块管理器那些蚕食用来配置模块,同样也指定模块相关一些其他信息。


mod_helloworld.xml 代码如下:

<?xml version="1.0" encoding="utf-8"?>
<install type="module" version="1.5.0">
    <name>Hello, World!</name>
    <author>John Doe</author>
    <version>1.5.0</version>
    <description>A simple Hello, World! module.</description>
    <files>
        <filename module="mod_helloworld">mod_helloworld.php</filename>
        <filename>index.html</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
    </files>
    <params>
    </params>
</install>


你可能注意到了有两个附件文件index.html 和 tmpl/index.html. 这两个文件主要是浏览目录的时候显示这两个文件,而不是显示目录下的文件结构。这两个文件仅仅包含一行:

<html><body bgcolor="#FFFFFF"></body></html>

就是一个空白页面

因为我们的模块没有参数,所以xml文件中这部分没有


结论:

Joomla 的模块开发是一个非常简单明了的过程,使用本教程中的技术,简单的变化能生成无尽的模块。



收藏此文章:
Digg! Reddit! Del.icio.us! JoomlaVote! Google! Live! Facebook! StumbleUpon! Yahoo! Free social bookmarking plugins and extensions for Joomla! websites!

发表您的文章评论

您的姓名 (昵称)
标题:
评分: 很差一般较好很好
评论:
验证码:
请输入验证码