joomla


28
Jun 10

Write a simple Joomla 1.6 module from scratch

Create you first Joomla 1.6 module completely from scratch.

Joomla, as of version 1.5 which was a complete rewrite is based on the MVC design pattern which makes creating modules extremely easy and structured. Joomla 1.6 changes a few things, the basics however remain the same.

  1. Create a folder called mod_helloworld in modules
  2. Create a mod_helloworld.php file
  3. Create a mod_helloworld.xml file
  4. Create a helper.php file
  5. Create a template file tmpl/default.php
  6. Discover new modules by going to “Extension Manager” -> “Discover”

The code for mod_helloworld.php

<?php
defined('_JEXEC') or die; // no direct access allowed
 
require_once dirname(__FILE__).DS.'helper.php'; // get helper files
 
$hello = modHelloWorldHelper::getHello($params);
require JModuleHelper::getLayoutPath('mod_helloworld');
?>

The helper.php file

<?php
	class modHelloWorldHelper
	{
	    /**
	     * Retrieves the hello message
	     *
	     * @param array $params An object containing the module parameters
	     * @access public
	     */    
	    function getHello( $params )
	    {
	        return 'Hello, World!';
	    }
	}
?>

Include the template file for the default view

<?php
defined('_JEXEC') or die;
echo $hello; 
?>

The helloworld.xml file

	<?xml version="1.0" encoding="utf-8"?>
	<extension type="module" version="1.6.0" client="site" method="upgrade">
	    <name>Hello World!</name>
	    <author>Reinhold Weber</author>
	    <version>1.6.0</version>
	    <description>Reinholds simple Hello World module.</description>
	    <files>
			 <filename module="mod_helloworld">mod_helloworld.php</filename>
	        <filename>mod_helloworld.xml</filename>
	        <filename>index.html</filename>
	        <filename>helper.php</filename>
	        <filename>tmpl/default.php</filename>
	        <filename>tmpl/index.html</filename>
	    </files>
	    <params>
	    </params>
	</extension>

Create both index.html files to prevent direct directory browsing

<html><body></body></html>