Rvsjoen/tutorial/Developing an MVC Component/Part 15

From Joomla! Documentation

< User:Rvsjoen‎ | tutorial/Developing an MVC Component

Adding a script file

Now you may say to yourself, I want my component to do awesome things during install and uninstall, and fear not, there is a simple way of accomplishing this. By implementing a script file for your component, certain functions will be executed during certain stages of installation/uninstallation. There are in total five hooks available and they are all except one, namely the update() hook, demonstrated in the below script file.

With your favorite editor, create the following file


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

class com_helloWorldInstallerScript
        function install($parent) 

        function uninstall($parent) 
                echo '<p>' . JText::_('COM_HELLOWORLD_UNINSTALL_TEXT') . '</p>';

        function update($parent) 
                echo '<p>' . JText::_('COM_HELLOWORLD_UPDATE_TEXT') . '</p>';

        function preflight($type, $parent) 
                echo '<p>' . JText::_('COM_HELLOWORLD_PREFLIGHT_' . $type . '_TEXT') . '</p>';

        function postflight($type, $parent) 
                echo '<p>' . JText::_('COM_HELLOWORLD_POSTFLIGHT_' . $type . '_TEXT') . '</p>';

Updating the language file

Since this is a script that will run with the installer, we need to put the language strings in the sys.ini file this time.


COM_HELLOWORLD_DESCRIPTION="This is the Hello World description"
COM_HELLOWORLD_HELLOWORLD_VIEW_DEFAULT_DESC="This view displays a selected message"
COM_HELLOWORLD_INSTALL_TEXT="HelloWorld Install script"
COM_HELLOWORLD_POSTFLIGHT_DISCOVER_INSTALL_TEXT="HelloWorld postlight discover install script"
COM_HELLOWORLD_POSTFLIGHT_INSTALL_TEXT="HelloWorld postflight install script"
COM_HELLOWORLD_POSTFLIGHT_UNINSTALL_TEXT="HelloWorld postflight uninstall script"
COM_HELLOWORLD_POSTFLIGHT_UPDATE_TEXT="HelloWorld postflight update script"
COM_HELLOWORLD_PREFLIGHT_DISCOVER_INSTALL_TEXT="HelloWorld preflight discover install script"
COM_HELLOWORLD_PREFLIGHT_INSTALL_TEXT="HelloWorld preflight install script"
COM_HELLOWORLD_PREFLIGHT_UNINSTALL_TEXT="HelloWorld preflight uninstall script"
COM_HELLOWORLD_PREFLIGHT_UPDATE_TEXT="HelloWorld preflight update script"
COM_HELLOWORLD_UNINSTALL_TEXT="HelloWorld Uninstall script"
COM_HELLOWORLD_UPDATE_TEXT="HelloWorld Update script"

Installation manifest

Now all we have to do is to tell the installation manifest that we have a script file and where it is.


<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="2.5.0" method="upgrade">

        <!-- The following elements are optional and free of formatting constraints -->
        <creationDate>June 2011</creationDate>
        <author>John Doe</author>
        <copyright>Copyright Info</copyright>
        <license>License Info</license>
        <!--  The version string is stored in the components table -->
        <!-- The description is optional and defaults to the name -->


                        <file driver="mysql" charset="utf8">sql/install.mysql.utf8.sql</file>
                        <file driver="mysql" charset="utf8">sql/uninstall.mysql.utf8.sql</file>

        <!-- Note the folder attribute: This attribute describes the folder
                to copy FROM in the package to install therefore files copied
                in this section are copied from "site/" in the package -->
        <files folder="site">

        <media destination="com_helloworld" folder="media">

                <menu img="../media/com_helloworld/images/tux-16x16.png">COM_HELLOWORLD_MENU</menu>
                        <menu view="helloworlds">COM_HELLOWORLD_SUBMENU_MESSAGES</menu>
                        <menu link="option=com_categories&amp;view=categories&amp;extension=com_helloworld">COM_HELLOWORLD_SUBMENU_CATEGORIES</menu>
                <!-- Note the folder attribute: This attribute describes the folder
                        to copy FROM in the package to install therefore files copied
                        in this section are copied from "admin/" in the package -->
                <files folder="admin">


Testing your component

For details on how to install the component into your Joomla! site, refer to the information provided in Part 01.

File listing

Download this part

Download example package

Articles in this series

This tutorial is supported by the following versions of Joomla!

Joomla 2.5