MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.


I'm not curious

How to Create a Simple Module with Joomla?

Published on 14 July 15
0
1
How to Create a Simple Module with Joomla? - Image 1

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
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);

The completed version of mod_helloworld.php file would appear as below
<?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:

Joomla

 
Post a Comment

Please notify me the replies via email.

Important:
  • We hope the conversations that take place on MyTechLogy.com will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
You may also be interested in
 
Awards & Accolades for MyTechLogy
Winner of
REDHERRING
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url

Back to Top