Actions

J3.x

Developing a MVC Component/Adding an install-uninstall-update script file

From Joomla! Documentation

< J3.x:Developing a MVC Component


This is a multiple-article series of tutorials on how to develop a Model-View-Controller Component for Joomla! Version Joomla 3.x.

Begin with the Introduction, and navigate the articles in this series by using the navigation box to the right (the Articles in this series).

Copyedit.png
This Page Needs Your Help

This page is tagged because it NEEDS REVIEW. You can help the Joomla! Documentation Wiki by contributing to it.
More pages that need help similar to this one are here. NOTE-If you feel the need is satistified, please remove this notice.


This is a multiple-article series of tutorials on how to develop a Model-View-Contoller Component for Joomla! Version Joomla 3.3.

Creating the extension script file

Installing, updating and uninstalling a component may require additional operations that cannot be achieved by the basic operations described in the main xml file. Joomla offers a new approach to solve this problem. It consists in using a php script file containing a class using five methods:

  • preflight which is executed before install and update
  • install
  • update
  • uninstall
  • postflight which is executed after install and update

Writing an extension script consists in declaring an class whose name is com_ComponentNameInstallerScript with these 5 methods. For plugins you need to add the group.


<?php
// No direct access to this file
defined('_JEXEC') or die;
 
/**
 * Script file of HelloWorld plugin
 * Group: Universe
 */
class plgUniverseHelloWorldInstallerScript
{
        /**
         * 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>';
        }
}