Joomla 3.x_component如何打包安裝和卸載

本文是component的打包,與component安裝和卸載相關的內容。component包內與安裝卸載相關的文件就只有兩個,script.php和componentname.xml。script.php是安裝或卸載時運行的腳本,componentname.xml是指導安裝如何進行的和包含哪些安裝文件。下面就一步步建一個基本component包,例子的component名爲com_example:

(1)example.xml文件,注意的是,每個文件夾下一定要有個index.html,防止目錄瀏覽。

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.0" method="upgrade">
  
  <!-- component的基本信息 -->
  <name>COM_EXAMPLE</name> <!-- 大寫表明該項爲多語言翻譯項,在language的ini文件中 -->
  <author>Laixiang Wen</author>
  <version>1.0</version>
  <creationDate>Apirl 4, 2013</creationDate>
  <copyright>Copyright (C) 2013 Fighting Bull Studio. All rights reserved.</copyright>
  <license>http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL</license>
  <authorEmail>[email protected]</authorEmail>
  <description>COM_EXAMPLE_XML_DESCRIPTION</description>
 
 <!-- 與安裝卸載有關的文件 -->
 <scriptfile>script.php</scriptfile><!-- 安裝過程中的高級定製 -->
  <install>
    <sql>
      <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file> <!-- 數據庫中表的創建 -->
    </sql>
  </install>
  <uninstall>
    <sql>
      <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file> <!-- 數據庫中表的刪除 -->
    </sql>
  </uninstall>
  
 	<!--安裝中拷貝的文件  -->
	<!-- frontend安裝的文件,放到WebRoot/components/com_example下 -->
  <files folder="site">
    <filename>index.html</filename>
    <filename>example.php</filename>
    <filename>controller.php</filename>  
    <folder>models</folder>
    <folder>views</folder>
    <folder>controllers</folder>
    <folder>helpers</folder>
  </files>
  <languages folder="site">
    <language tag="en-GB">language/en-GB/en-GB.com_example.ini</language>
  </languages>
  
  <!-- 媒體文件放置的目錄 ,創建並放置到media/com_example目錄-->
  <media destination="com_example" folder="media">
    <filename>index.html</filename>
    <folder>images</folder>
  </media>
  
  <!-- backend相關的文件及設置 -->
  <administration>
  <!-- 在後臺的component菜單中創建該component的菜單項 -->
    <menu img="class:example">COM_EXAMPLE_MENU</menu>
    <submenu>
	  <!-- Note that all & must be escaped to & for the file to be valid XML 
		and be parsed by the installer -->
	  <menu link="option=com_example" view="links" img="class:example"
		alt="example/Submenu1">COM_EXAMPLE_SUBMENU_1</menu>
	  <menu link="option=com_categories&extension=com_example"
		view="categories" img="class:example-cat" alt="Example/submenu2">COM_EXAMPLE_SUBMENU_2</menu>
    </submenu>
    
    <!-- backend安裝的文件,放到WebRoot/administrator/components/com_example下 -->
    <files folder="admin">
      <filename>index.html</filename>
      <filename>example.php</filename><!-- 該組件的入口文件 -->
      <filename>access.xml</filename><!-- 控制角色權限的文件 -->
      <filename>config.xml</filename><!-- 該組件的options項設置 -->
      <filename>controller.php</filename>    
      <folder>controllers</folder>
      <folder>models</folder>
      <folder>views</folder>
      <folder>tables</folder>
      <folder>helpers</folder>
      <folder>sql</folder>
    </files>
    
    <languages folder="admin">
      <language tag="en-GB">language/en-GB/en-GB.com_example.ini</language>
      <language tag="en-GB">language/en-GB/en-GB.com_example.sys.ini</language>
    </languages>
  </administration>
</extension>

(2)script.php文件,類名一定要爲com_componentnameInstallerScript,這裏是com_exampleInstallerScript

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

/**
 * Script file of EXAMPLE component
 */
class com_exampleInstallerScript
{
	/**
	 * method to install the component
	 *
	 * @return void
	 */
	function install($parent)
	{
		// $parent is the class calling this method
		//$parent->getParent()->setRedirectURL('index.php?option=com_example');
	}

	/**
	 * method to uninstall the component
	 *
	 * @return void
	 */
	function uninstall($parent)
	{
		// $parent is the class calling this method
		echo '<p>' . JText::_('COM_EXAMPLE_UNINSTALL_TEXT') . '</p>';
	}

	/**
	 * method to update the component
	 *
	 * @return void
	 */
	function update($parent)
	{
		// $parent is the class calling this method
		echo '<p>' . JText::sprintf('COM_EXAMPLE_UPDATE_TEXT', $parent->get('manifest')->version) . '</p>';
	}

	/**
	 * method to run before an install/update/uninstall method
	 *
	 * @return void
	 */
	function preflight($type, $parent)
	{
		// $parent is the class calling this method
		// $type is the type of change (install, update or discover_install)
		echo '<p>' . JText::_('COM_EXAMPLE_PREFLIGHT_' . $type . '_TEXT') . '</p>';
	}

	/**
	 * method to run after an install/update/uninstall method
	 *
	 * @return void
	 */
	function postflight($type, $parent)
	{
		// $parent is the class calling this method
		// $type is the type of change (install, update or discover_install)
		echo '<p>' . JText::_('COM_EXAMPLE_POSTFLIGHT_' . $type . '_TEXT') . '</p>';
	}
}


3)下圖爲目錄及文件的組織,文件下載地址:

http://download.csdn.net/detail/fightingbull/5221439



 

輝輝        

(FightingBull Studio)

 

歡迎轉載,但請註明出處:http://blog.csdn.net/fightingbull




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章