php – Joomla – 自訂編寫的模組 (module) 的安裝方式

Joomla 模組架構
- mod_helloworld/ 官網案例命名為「mod_自訂模板名稱」
- sql/ 放置安裝/移除套件所需要的 SQL
- mysql/ 範例使用 MySQL
- updates/ 若要更新套件所要修改的 SQL 放置處
- install.mysql.utf8.sql 新增模組會用到的 SQL
- uninstall.mysql.utf8.sql 移除模組會用到的 SQL
- mysql/ 範例使用 MySQL
- tmpl/ 模板的路徑
- default.php 顯示的 View
- index.html 避免直接讀取的阻擋警告
- helper.php 也就是 Library,提供給 mod_helloworld.php 呼叫,也就是 Model
- index.html 避免直接讀取的阻擋警告
- mod_helloworld.php 模組進入點,也就是 Controller 的部分
- mod_helloworld.xml 關於模組的資訊,例如模組名稱、版本、安裝的檔案路徑……
- sql/ 放置安裝/移除套件所需要的 SQL
依序建立新模板
要注意模組的後臺顯示與前台顯示的方式不同
- 後台:使用 mod_helloworld.xml 中的 <config> 作為設定,取得 SQL 的編碼寫在 XML 的屬性中。
- 前台:顯示結果的部分則是靠 tmpl/default.php 顯示,取得 SQL 的編碼寫在 helper.php 中。
modules/mod_helloworld/mod_helloworld.xml
這個 xml 是基本資料與對應的路徑名稱,還有提供給後台設定的欄位方式,可以觀察註解。關於 <field type=””> 的屬性甚至是其他的屬性,可以查閱文件,不同的值可以直接產出不同的功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
<?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> <fields name="params"> <fieldset name="basic"> <!-- 後台設定中,欄位顯示的方式,會以 <select> 呈現 --> <!-- name="lang" 需要對應 select 欄位名稱 --> <!-- queue="" 中以 id as value 的 value 會自動添加到下拉式選單的 value="" --> <field name="lang" type="sql" default="1" label="Select a language" query="SELECT id AS value, lang FROM #__helloworld" /> </fieldset> </fields> </config> <!-- 指定 SQL 的路徑 --> <folder>sql</folder> <!-- install/uninstall/update 所需要的檔案路徑設定 --> <install> <sql> <file driver="mysql" charset="utf8">sql/mysql/install.mysql.utf8.sql</file> <file driver="sqlazure" charset="utf8">sql/sqlazure/install.sqlazure.utf8.sql</file> </sql> </install> <uninstall> <sql> <file driver="mysql" charset="utf8">sql/mysql/uninstall.mysql.utf8.sql</file> <file driver="sqlazure" charset="utf8">sql/sqlazure/uninstall.sqlazure.utf8.sql</file> </sql> </uninstall> <update> <schemas> <schemapath type="mysql">sql/mysql/updates</schemapath> <schemapath type="sqlazure">sql/sqlazure/updates</schemapath> </schemas> </update> </extension> |
modules/mod_helloworld/sql/mysql/install.mysql.utf8.sql
安裝時的 SQL
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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'); |
modules/mod_helloworld/sql/mysql/uninstall.mysql.utf8.sql
反安裝時要移除的 SQL
1 2 3 4 |
# Placeholder file for database changes for version 1.0.0 DROP TABLE IF EXISTS `#__helloworld` |
modules/mod_helloworld/mod_helloworld.php
控制器的部分,也就是進入點。當前台網址取得 lang=X 的時候,將參數帶入 getHello() 。
1 2 3 4 5 6 7 8 9 10 |
<?php defined('_JEXEC') or die; require_once dirname(__FILE__) . '/helper.php'; $language = $params->get('lang', '1'); $hello = modHelloWorldHelper::getHello( $language ); require JModuleHelper::getLayoutPath('mod_helloworld'); |
modules/mod_helloworld/helper.php
這裡呼叫資料表中的資料,準備提供給前台使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php class ModHelloWorldHelper { public static function getHello($params) { // Obtain a database connection $db = JFactory::getDbo(); // Retrieve the shout - note we are now retrieving the id not the lang field. $query = $db->getQuery(true) ->select($db->quoteName('hello')) ->from($db->quoteName('#__helloworld')) ->where('id = '. $db->Quote($params)); // Prepare the query $db->setQuery($query); // Load the row. $result = $db->loadResult(); // Return the Hello return $result; } } |
modules/mod_helloworld/tmpl/default.php
View 的部分,也是顯示給前台使用
1 2 3 4 5 6 |
<?php // No direct access defined('_JEXEC') or die; ?> <h3><?php echo $hello; ?></h3> |
進入後台安裝套件
上方的程式碼放在「你的專案資料夾/modules/mod_helloworld」以後,我們先從後台 探索 並點擊安裝
點選 Hello, World! 就會開始新增這個模組提供給我們使用。記得這個程序是 「安裝 > 新增」。接著會看到我們要設定的語言。這個下拉選單,就是由 mod_helloworld.php 中的 <field> 屬性寫入。
我們可以在右側的部分,設定在想要顯示的前台位置,例如我們顯示在頁尾,接著按下儲存。記得標題要填寫。
這樣就把紀錄寫在資料表 #__modules 中儲存了。可以前往資料庫查看紀錄。若要取出儲存在後台的欄位值,就要在 Helper 的部分用 SQL 取出 #__modules 的欄位 params 並解碼 JSON 格式。因為後台儲存管理者填寫的資料,都是被 Joomla! 製成 JSON 後才存放到欄位 params。
-接下來我前往前台的 Footer 頁尾,就能看到這個模組顯示在我們要的位置上
edp2100
2018-04-12 - 15:28
感謝您的分享,很詳細的教學,不過我有點想換 laravel了XD
JSN
2018-04-12 - 16:52
哈哈我是因為新公司使用 Joomla! 套版開發的關係,所以現在才接觸XD
如果可以的話還是 Framework 開發比較好掌握跟維護。