How to Create a Simple Module with Joomla?
Published on 14 July 15
0
1
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
The helper class is defined in the helper.php file which will be included using the require_once statement as below
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);
<?php
/**
* Hello World! Module Entry Point
*
* @package STTL
* @subpackage Modules
* @link http://docs.joomla.org/J2.5:Creating_a_simple_module/Developing_a_Basic_Module
* @license GNU/GPL, see LICENSE.php
* mod_helloworld is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/
// No direct access
defined('_JEXEC' ) or die;
// Include the syndicate functions only once
require_once dirname(__FILE__) . '/helper.php';
$hello = modHelloWorldHelper::getHello($params);
require(JModuleHelper::getLayoutPath('mod_helloworld'));
?>Creating Helper.php
The helper class defined will include a single method getHello() which will display the message hello world
<?php
/**
* Helper class for Hello World! module
*
* @package STTL
* @subpackage Modules
* @link docs.joomla.org/J2.5:Creating_a_simple_module/Developing_a_Basic_Module
* @license GNU/GPL, see LICENSE.php
* mod_helloworld is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
*/
class ModHelloWorldHelper
{
/**
* Retrieves the hello message
*
* @param array $params An object containing the module parameters
* @access public
*/
public static function getHello( $params )
{
return 'Hello, World!';
}
}
?> Creating the Default Template
You will need to create the default.php file, which will display the module output. Here is the code that will help you create the template
<?php
// No direct access
defined('_JEXEC') or die; ?>
<?php echo $hello; ?>
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
<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="2.5.0" client="site" method="upgrade">
<name>Hello, World!</name>
<author>John Doe</author>
<version>1.0.0</version>
<description>A simple Hello, World! module.</description>
<files>
<filename>mod_helloworld.xml</filename>
<filename module="mod_helloworld">mod_helloworld.php</filename>
<filename>index.html</filename>
<filename>helper.php</filename>
<filename>tmpl/default.php</filename>
<filename>tmpl/index.html</filename>
</files>
<config>
</config>
</extension>
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.
This blog is listed under
Open Source
and Development & Implementations
Community
Related Posts:
Post a Comment
You may also be interested in
Share your perspective
Share your achievement or new finding or bring a new tech idea to life. Your IT community is waiting!