joomla 3.x 创建简单的模块

(一)开发一个基本的模块

说明:
joomla中的模块是轻量的可伸缩的扩展。它们通常用于最少的页面输出,有点复杂并通穿插在不同的组件中。

文件结构:
在标准的joomla模块开发中有如下几个基本的文件,比如你开发的是一个helloworld的模块
mod_helloworld.php 这个主要是模块的入口文件。它执行的是任何需要的初始化程序及调用帮助类程序收集数据,还有调用模块中输出需要的模板
mod_helloworld.xml 这是模块信息定义文件。它定义了一些需要安装的文件及模块需要的参数定义。
helper.php 这个文件里面包涵了给模块检索信息并展示的帮助类。
tmpl/default.php 模块的模板。就是如何展示模块收集的数据

创建 mod_helloworld.php
mod_helloworld.php 文件有三个任务
1.引入 helper.php 文件
2.调用helper.php 帮助类来检索需要的数据
3.引入模板来展示检索的数据
文件内容如下:

<?php
#确认该文件是被joomla引入的,而不是被直接访问的
defined('_JEXEC') or die;
引入帮助类文件
require_once dirname(__FILE__) . '/helper.php';

调用帮助类中的方法
$hello = modHelloWorldHelper::getHello($params);
引入JModuleHelper中的getLayoutPath方法来找到该模块的模板文件来处理该模块中的数据,如$hello
require JModuleHelper::getLayoutPath('mod_helloworld');

创建helper.php

<?php
class ModHelloWorldHelper
{
    public static function getHello($params)
    {
        return 'Hello, World!';
    }
}

创建tmpl/default.php

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

创建mod_helloworld.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1.0" client="site" method="upgrade">
    <name>Hello, World!</name>
    <author>John Doe</author>
    <version>1.0.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>
    <config>
    </config>
</extension>


(二)joomla基本的模块中引用数据库

在这部分中需要了解JDatabase的使用,可参考:https://docs.joomla.org/Accessing_the_database_using_JDatabase

在安装时创建表,即在文件sql/mysql/install.mysql.utf8.sql中创建表

CREATE TABLE IF NOT EXISTS `#__helloworld` (
    `id` int(10) NOT NULL AUTO_INCREMENT,
    `hello` text NOT NULL,
    `lang` varchar(25) NOT NULL,

  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;

INSERT INTO `#__helloworld` (`hello`, `lang`) VALUES ('Hello World', 'en-GB');
INSERT INTO `#__helloworld` (`hello`, `lang`) VALUES ('Hola Mundo', 'es-ES');
INSERT INTO `#__helloworld` (`hello`, `lang`) VALUES ('Bonjour tout le monde', 'fr-FR');

然后需要补充卸载时需要执行的sql,即文件sql/mysql/uninstall.mysql.utf8.sql中写入卸载时需要执行的sql

DROP TABLE IF EXISTS `#__helloworld`

当然后后期开发中有对数据库更新更新可以在sql/mysql/updates中添加更新sql文件,文件命名方式为版本号.sql 如:2.0.0.sql

扫描二维码关注公众号,回复: 3332025 查看本文章

修改helper.php 文件中的help类
内容如下:

<?php
class ModHelloWorldHelper
{
    public static function getHello($params)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true)
            ->select($db->quoteName('hello'))
            ->from($db->quoteName('#__helloworld'))
            ->where('lang = ' . $db->Quote('en-GB'));
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result;
    }
}

最后修改mod_helloworld.xml中把需要的文件及文件的作用定义好

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1.0" client="site" method="upgrade">
    <name>Hello, World!</name>
    <author>John Doe</author>
    <version>4.0.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>

        <folder>sql</folder>
    </files>

    <install>
        <sql>
            <file driver="mysql" charset="utf8">sql/mysql/install.mysql.utf8.sql</file>
        </sql>
    </install>

    <uninstall>
        <sql>
            <file driver="mysql" charset="utf8">sql/mysql/uninstall.mysql.utf8.sql</file>
        </sql>
    </uninstall>

    <update>
        <schemas>
            <schemapath type="mysql">sql/mysql/updates</schemapath>
        </schemas>
    </update>

    <config>

    </config>
</extension>

(三)给joomla模块中添加表单字段
其实就是给模块的显示添加参数控制
首先在mod_helloworld.xml文件中的<config>元素里添加如下

<fields name="params">
    <fieldset name="basic">
        <field
               name="lang"
               type="sql"
               default="1"
               label="Select a language"
               query="SELECT id AS value, lang FROM #__helloworld" />
    </fieldset>
</fields>

上面这个是SQL表单字段的写法,可参数https://docs.joomla.org/Special:MyLanguage/SQL_form_field_type
然后修改mod_helloworld.php中的内容如下

<?php
defined('_JEXEC') or die;
require_once dirname(__FILE__) . '/helper.php';

#$params 是mod_helloworld.xml中的字段参数配置名,其中lang是其中的一个参数字段名,
#它的值是SELECT id AS value, lang FROM #__helloworld这个表里查询出的id的值 
$language = $params->get('lang', '1');

$hello    = modHelloWorldHelper::getHello( $language );

require JModuleHelper::getLayoutPath('mod_helloworld');

最后修改helper.php文件,内容如下

<?php
class ModHelloWorldHelper
{
    public static function getHello($params)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true)
            ->select($db->quoteName('hello'))
            ->from($db->quoteName('#__helloworld'))
            ->where('id = ' . $db->Quote($params));
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result;
    }
}

最后mod_helloworld.xml的内容如下:

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1.0" client="site" method="upgrade">
    <name>Hello, World!</name>
    <author>John Doe</author>
    <version>4.0.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>

        <folder>sql</folder>
    </files>

    <install>
        <sql>
            <file driver="mysql" charset="utf8">sql/mysql/install.mysql.utf8.sql</file>
        </sql>
    </install>

    <uninstall>
        <sql>
            <file driver="mysql" charset="utf8">sql/mysql/uninstall.mysql.utf8.sql</file>
        </sql>
    </uninstall>

    <update>
        <schemas>
            <schemapath type="mysql">sql/mysql/updates</schemapath>
        </schemas>
    </update>

    <config>
        <fields name="params">
            <fieldset name="basic">
                <field
                        name="lang"
                        type="sql"
                        default="1"
                        label="Select a language"
                        query="SELECT id AS value, lang FROM #__helloworld1" />
            </fieldset>
        </fields>
    </config>
</extension>

猜你喜欢

转载自blog.51cto.com/quietnight/2283678