J3.x

建立一個簡單的模組/新增一個安裝/移除/更新程式碼檔案

From Joomla! Documentation

< J3.x:Creating a simple module
This page is a translated version of the page J3.x:Creating a simple module/Adding an install-uninstall-update script file and the translation is 70% complete.

Other languages:
English • ‎español • ‎français • ‎Bahasa Indonesia • ‎Nederlands • ‎português • ‎中文(台灣)‎
Joomla! 
3.x
教學
建立一個簡單的模組

這一系列的文章介紹如何 建立一個 Joomla! 模組 版本 Joomla 3.x。您可以依序瀏覽文章。

讓我們從簡介開始,您可以使用底下的導覽按鈕來瀏覽文章,或是右側的方塊中的連結(列出所有的文章)。 您可以觀看 2 部和本教學相關的影片,基本 Joomla 模組開發影片教學 1 and 基本 Joomla 模組開發影片教學 2.




介紹

安裝、移除模組可能需要一些額外的操作,無法被包裝於.xml 檔案。Joomla! 提供了一個新的方法來解決這個問題。使用PHP程式碼來使用五個功能,包含class:

  • preflight which is executed before install and update
  • 安裝
  • 更新
  • 移除
  • postflight which is executed after install and update

Creating the Extension Script File

Write an extension script by declaring a class whose name is mod_ModuleNameInstallerScript with these five functions.

script.php

<?php
// No direct access to this file
defined('_JEXEC') or die;

/**
 * Script file of HelloWorld module
 */
class mod_helloWorldInstallerScript
{
	/**
	 * Method to install the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function install($parent) 
	{
		echo '<p>The module has been installed.</p>';
	}

	/**
	 * Method to uninstall the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function uninstall($parent) 
	{
		echo '<p>The module has been uninstalled.</p>';
	}

	/**
	 * Method to update the extension
	 * $parent is the class calling this method
	 *
	 * @return void
	 */
	function update($parent) 
	{
		echo '<p>The module has been updated to version' . $parent->get('manifest')->version . '.</p>';
	}

	/**
	 * Method to run before an install/update/uninstall method
	 * $parent is the class calling this method
	 * $type is the type of change (install, update or discover_install)
	 *
	 * @return void
	 */
	function preflight($type, $parent) 
	{
		echo '<p>Anything here happens before the installation/update/uninstallation of the module.</p>';
	}

	/**
	 * Method to run after an install/update/uninstall method
	 * $parent is the class calling this method
	 * $type is the type of change (install, update or discover_install)
	 *
	 * @return void
	 */
	function postflight($type, $parent) 
	{
		echo '<p>Anything here happens after the installation/update/uninstallation of the module.</p>';
	}
}

In the update method we show the new version number using $parent->get('manifest')->version. You can also redirect to a page of your choice with $parent->getParent()->setRedirectURL('index.php?option=com_modules');.

Remember to add a call to your script.php in the module's .xml file: <scriptfile>script.php</scriptfile>