• Joomla Video Tutorials

Joomla - 建立模組



在本章中,我們將學習在 Joomla 中**建立模組**。模組是靈活且輕量級的擴充套件,可用於頁面渲染。

建立模組

以下是建立 Joomla 模組的簡單步驟。

**步驟 1** - 在您的 **Joomla** → **modules** 資料夾中建立一個名為 **mod_firstmodule** 的資料夾。

Joomla Create Modules

**步驟 2** - 在 **mod_firstmodule** 資料夾中建立一個名為 "helper.php" 的檔案。此檔案包含名為 helper 的類名,它有助於在模組輸出中顯示檢索到的資料。

helper.php

<?php
   /**
      * Helper class for Hello World! module
      *
      * @package    Joomla.Tutorials
      * @subpackage Modules
      * @link http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
      * @license        GNU/GPL, see LICENSE.php
      * mod_helloworld is free software. This version may have been modified pursuant
      * to the GNU General Public License, and as distributed it includes or
      * is derivative of works licensed under the GNU General Public License or
      * other free or open source software licenses.
   */
		
   class ModHelloWorldHelper {
      /**
         * Retrieves the hello message
         *
         * @param   array  $params An object containing the module parameters
         *
        * @access public
      */
		
      public static function getHello($params) {
         return 'Hello, World!';
      }
   }
	
?>

**步驟 3** - 建立一個名為 **mod_helloworld.php** 的檔案。它是模組的入口點,執行初始化例程,收集必要的資料並使用模板顯示模組輸出。

mod_helloworld.php

<?php
   /**
      * Hello World! Module Entry Point
      *
      * @package    Joomla.Tutorials
      * @subpackage Modules
      * @license    GNU/GPL, see LICENSE.php
      * @link       http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
      * mod_helloworld is free software. This version may have been modified pursuant
      * to the GNU General Public License, and as distributed it includes or
      * is derivative of works licensed under the GNU General Public License or
      * other free or open source software licenses.
   */

   // No direct access
   defined('_JEXEC') or die;

   // Include the syndicate functions only once
   require_once dirname(__FILE__) . '/helper.php';

   $hello = modHelloWorldHelper::getHello($params);
   require JModuleHelper::getLayoutPath('mod_helloworld');
?>

**步驟 4** - 建立一個 **mod_helloworld.xml 檔案**。此檔案包含有關模組的資訊。此 xml 檔案包含要為模組安裝到 Joomla 中的檔案的資訊。

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>Tutorials Point</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>

**步驟 5** - 建立一個名為 **index.html** 的簡單 html 檔案。編寫此檔案的目的是,不允許瀏覽建立的目錄。當用戶瀏覽到這些目錄時,將顯示 index.html 檔案。您甚至可以將此檔案保留為空。

index.html

<html>
   <body> Welcome to Tutorials Point!!!!! </body>
</html>

**步驟 6** - 建立一個名為 **tmpl** 的資料夾。將 **default.php** 檔案(如下所示)和 index.html(在步驟 (5) 中建立)放置在 **tmpl** 資料夾下。default.php 檔案是一個顯示模組輸出的模板。

default.php

<?php
   /**
      * @package Joomla.Site
      * @subpackage mod_firstmodule
      * @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
      * @license GNU General Public License version 2 or later; see LICENSE.txt
   */
defined('_JEXEC') or die;
>

<p>Hello World!!!!!!</p>

完成所有這些檔案的建立後,壓縮整個 **mod_firstmodule** 資料夾。

**步驟 7** - 轉到 Joomla 管理員中的 **擴充套件** → **擴充套件管理器**,您將看到以下螢幕。在這裡,您可以上傳和安裝您建立的模組檔案,即 **mod_firstmodule** 資料夾。單擊 **選擇檔案** 並選擇建立的模組檔案(壓縮檔案)。單擊 **上傳並安裝** 按鈕以上傳模組檔案。

Joomla Create Modules

**步驟 8** - 上傳和安裝後,轉到 **模組管理器** 並單擊 **新建**。在那裡,您可以檢視您建立的模組檔案,如下所示。

Joomla Create Modules

**步驟 9** - 您可以像分配其他模組一樣分配此模組,然後釋出它。

廣告