基本上Joomla!模組都是放在 modules/mod_example
之下,最少僅需三個檔案,分別是:
- modules/mod_example/mod_example.php
- modules/mod_example/mod_example.xml
- modules/mod_example/tmpl/default.php
不過大多數模組會再加上一個helper來撈取資料庫。
相關程式碼如下:
mod_example.xml
<?xml version="1.0" encoding="utf-8"?>
<extension
type="module"
version="2.5"
client="site"
method="upgrade">
<name>Module Example</name>
<author>Your Name</author>
<creationDate>2012-04-11</creationDate>
<copyright>Copyright (C) 2005 - 2011 example.com</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>example@example.com</authorEmail>
<authorUrl>http://example.com</authorUrl>
<version>1.0</version>
<description>MOD_EXAMPLE_INSTALL_DESC</description>
<files>
<filename module="mod_example">mod_example.php</filename>
<folder>tmpl</folder>
<folder>language</folder>
<filename>index.html</filename>
<filename>mod_example.xml</filename>
</files>
<help key="JHELP_EXTENSIONS_MODULE_MANAGER_FOOTER" />
<config>
<fields name="params">
<fieldset name="basic">
</fieldset>
<fieldset name="advanced">
<field
name="layout"
type="modulelayout"
label="JFIELD_ALT_LAYOUT_LABEL"
description="JFIELD_ALT_MODULE_LAYOUT_DESC" />
<field
name="moduleclass_sfx"
type="text"
label="COM_MODULES_FIELD_MODULECLASS_SFX_LABEL"
description="COM_MODULES_FIELD_MODULECLASS_SFX_DESC" />
<field
name="cache"
type="list"
default="1"
label="COM_MODULES_FIELD_CACHING_LABEL"
description="COM_MODULES_FIELD_CACHING_DESC">
<option
value="1">JGLOBAL_USE_GLOBAL</option>
<option
value="0">COM_MODULES_FIELD_VALUE_NOCACHING</option>
</field>
<field
name="cache_time"
type="text"
default="900"
label="COM_MODULES_FIELD_CACHE_TIME_LABEL"
description="COM_MODULES_FIELD_CACHE_TIME_DESC" />
<field
name="cachemode"
type="hidden"
default="static">
<option
value="static"></option>
</field>
</fieldset>
</fields>
</config>
</extension>
xml檔案專門用來放置該模組的基本資料,以及參數設定。
mod_example.php
<?php
// no direct access
defined('_JEXEC') or die;
// 載入helper幫忙撈取資料庫
require_once dirname(__FILE__).'/helper.php';
// 用helper取得想要的資料,放在 $list 中,template 中可以直接使用這個變數
$list = modExampleHelper::getList($params) ;
// 模組類別後綴字
$moduleclass_sfx = htmlspecialchars($params->get('moduleclass_sfx'));
// 取得模組template,預設為default
require JModuleHelper::getLayoutPath('mod_example', $params->get('layout', 'default'));
模組的入口檔案
helper.php
<?php
// no direct access
defined('_JEXEC') or die;
abstract class modExampleHelper
{
public static function getList(&$params)
{
$list = null;
return $list;
}
}
helper 可用來撈取或處理資料庫
tmpl/default.php
<?php
// no direct access
defined('_JEXEC') or die;
?>
<!-- 這裡可以直接用之前以helper取得的$list -->
<div>$list->title</div>
template是用來展示資料的html view,可以有多個不同的templates,在參數中選擇要使用哪一個
安裝
直接將所有檔案打包成 zip 檔即可安裝。也可以把檔案直接放到 modules/mod_example
下面,再到後台探索安裝。
範例檔
相關程式碼可以在Github上下載: