Joomla扩展模块开发文档(module)

本文介绍了 Joomla 中扩展模块的基本概念及其组成部分,包括模块的结构、文件组成及配置方法,并展示了如何通过示例代码实现一个简单的 Hello World 模块。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

************************************************************************

扩展模块用于简单的页面展示,可以应用于多个不同的组件

扩展模块可以使页面构建更加灵活并且可以提高程序的重用性

模块分为前台和后台两种

在配置文件的install元素中设置属性client="administrator"即为后台模块

前台的模块的目录为<root>/modules,后台模块的目录为<root>/administrator/modules

扩展组件目录的命名约定是mod_<Name>

文件

************************************************************************

一个基本的模块包括四个文件

mod_helloworld.php - 入口文件,执行相关初始化操作,通过helper.php获取数据并设置模板

mod_helloworld.xml - 配置文件,设置模块安装相关参数

helper.php - 一般是模块的一个helper类,进行实际的数据读取,逻辑运算的相关操作

tmpl/default.php - 模板文件,设置模块的显示效果

mod_helloworld.php

示例:

<?php
/**
 * Hello World! Module Entry Point
 * 
 * @package    Joomla.Tutorials
 * @subpackage Modules
 */
 
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
 
// 引入helper
require_once( dirname(__FILE__).DS.'helper.php' );

// 获取数据
$hello = modHelloWorldHelper::getHello( $params );
// 加载模板
require( JModuleHelper::getLayoutPath( 'mod_helloworld' ) );

helper.php

示例:

<?php
/**
 * Helper class for Hello World! module
 * 
 * @package    Joomla.Tutorials
 * @subpackage Modules
 */
class modHelloWorldHelper                // helper类的命名mod<name>Helper(非强制性的命名约定)
{
    /**
     * Retrieves the hello message
     *
     * @param array $params              // 一个填充了模块参数的JParameter对象
     * @access public
     */    
    function getHello( $params )
    {
    	$hello = $params->get( 'hello' );// 获取hello参数的值
        return $hello;
    }
}

tmpl/default.php

示例:

<?php // no direct access
defined( '_JEXEC' ) or die( 'Restricted access' ); ?>
<?php echo $hello; ?>

模板中可以直接使用入口文件中定义的变量

mod_helloworld.xml

示例:

<?xml version="1.0" encoding="utf-8"?>
<install type="module" version="1.5.0">
<!-- 扩展类型:type=[component|module|plugin] version=<joomla版本> [client="administrator"] 后台模块 [method="upgrade"] 以更新方式安装-->
    <name>Hello, World!</name>
    <version>1.5.0</version><!-- 扩展的版本信息 -->
    <description>A simple Hello, World! module.</description>
    <!-- 文件列表 -->
    <files>
        <filename>mod_helloworld.xml</filename>
        <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>
		<param name="hello" type="text" default="hello,world!" label="hello" description="hello string" />
    </params>
</install>

参数的相关设置可以应用于所有类型的扩展


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值