Tuesday, December 6, 2011

Joomla 1.7 - Create a Hello World Simple Module

  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 mod_helloworld.xml file

 <?xml version="1.0" encoding="utf-8"?>
 <extension type="module" version="1.7.0" client="site" method="upgrade">
     <name>Hello World!</name>
     <author>Harshal Shah </author>
     <version>1.7.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>

2 comments: