Home Tutorials Let there be light

Let there be light

  previous next
February 06, 2009 by Victor    

Let's create a Joomla Module! Just go to your Joomla installation path and create under the modules directory a new directory called mod_blank, blank this is the name of our first module. In there create two files, the mod_blank.xml file which describes the module and the mod_blank.php file which contains the actual code.

/var/www/joomla/modules/mod_blank/mod_blank.xml
/var/www/joomla/modules/mod_blank/mod_blank.php

Here is the xml file:

<?xml version="1.0" encoding="utf-8"?>
<install type="module" version="1.5.0">
        <name>blank</name>
        <author>me</author>
        <version>0.0.1</version>
        <description>TEST</description>
        <files>
                <filename module="mod_blank">mod_blank.php</filename>
        </files>
</install>

This will suffice for now, there’s not much in there, the XML file sets a few module attributes that are not mandatory and describes the required files that are related to the module. Looking at other modules you may find really long XML files, just remember for now that the important bits are the name and files tags. Onto the main code file.

<?php
   print "hello Joomla world";
?>

That’s it, yeap that’s all folks! Now, to see it happening you have to go to the backend, to the module manager under the extensions tab and press new. After selecting the blank module, give it a title and enable it. Remember to place it in a position that exists on your template. Should work.

Well, modules can be accessed from the outside (not through Joomla) and this is not a nice thing to have, perhaps it is alright for development but, certainly not for a production site. By hitting the following URL with your browser you can access the blank module directly.

http://yourserverip/joomla/modules/mod_blank/mod_blank.php

Let’s improve it a bit. Modify the mod_blank.php to the following:

<?php
   defined('_JEXEC') or die('Restricted access');
   echo JText::_('Hello World!');
?>

The first line restricts module access, now our module can be invoked only from the main Joomla code. In the second line, JText is a text handling class supporting text translation during runtime, it just returns the quoted string for now. In general it is good practice to use it, multilingual site developers will appreciate it a lot nevertheless I am not going to use it for the rest of the tutorial. Just mentioned it, in case you've been looking at other modules and you are wondering.

Add comment


Security code
Refresh