on 10 December 18
Modules are basically lightweight extensions that help in page rendering, thus improving the performance of your website. You will find your Joomla site loaded with different kinds of modules - news, menus etc. Let's have a look at how a simple module can be created using Joomla platform. For the sake of simplicity the "hello world" module has been considered.
Understanding the File Structure
Before you start creating the module, you will need to understand the file structure. There are four basic files that used in standard development methods
- mod_helloworld.php: This is the entry point for the module. You will be able to offer initialization routines, call the helper, and include the template to display module output using this file
- mod_helloworld.xml: This file is where the module related information will be stored. The files that need to be installed, and the required configuration parameters will be defined here
- helper.php: The helper class is located in this file, which is eventually used to retrieve information that will eventually be displayed in the module
- tmpl/default.php: This is the actual module template. The data to be displayed will be collected from the mod_helloworld.php and then a subsequent HTML file will be generated to display the output
Create the PHP File
You will need to create the mod_helloworld.php file. The file is scheduled to perform three major tasks
- Include the helper.php file which has the class that will collect the requisite data
- Introduce the helper class method which will help retrieve the data
- Include the template that will display the required output
require_once dirname(__FILE__) . '/helper.php';
The require_once is used to define the class only once
The helper class once defined will include only one method getHello(). To invoke the helper class, you will need to use the following method
$hello = modHelloWorldHelper::getHello($params);
The completed version of mod_helloworld.php file would appear as below
Creating the Hello World Module
This file will help specify the files that the installer needs to copy, and specify the parameters that need to be used to perform the copy
Here is the code that will help in creating the module
A simple Hello, World! module.
The files index.html and tmpl/index.html are no longer required to create this module. The config section will remain empty as there are no form fields required in here.