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

修改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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章