More Advanced Joomla Modules with PHP and Eclipse
Written by Administrator   
Wednesday, 11 November 2009
Article Index
More Advanced Joomla Modules with PHP and Eclipse
Helper and template

It's a good idea to adopt a lightweight Model View Controller (MVC) pattern  making use of  a helper file and a template

Banner

There is more to module creation than the simple module described in the introduction to Joomla modules.

Although you can stick with the simple one file module architecture described in that article it is advisable to move up to a more standard architecture for a Joomla module based on a lightweight Model View Controller (MVC) pattern.

Modules usually make use of two other files - a helper and a template - in addition to the main PHP code in mod_mymodule.php.

This is a lightweight architecture which tends to suit modules but there is no reason why you couldn't implement a more complex architecture such as the MVC framework typically used by a Joomla component.

The idea is that the helper file contains a helper class that the entry point uses to get the dirty work done - usually database access and the template takes this data and generates the HTML to be displayed in the modules location on the page.

It is also customary to create the template in a tmpl subdirectory - but you don't have to if you don't want to.

The manifest XML file

To expand the original example too include a helper and tempate the XML file that defines the files to be installed has to be altered to read:

<?xml version="1.0" encoding="utf-8"?>
<install type="module" version="1.5.0">
<name>mymodule</name>
<author>Author</author>
<version>1.5.0</version>
<files>
<filename>mod_mymodule.xml</filename>
<filename module="mod_mymodule">
mod_mymodule.php</filename>
<filename>index.html</filename>
<filename>helper.php</filename>
<filename>tmpl/default.php</filename>
<filename>tmpl/index.html</filename>
</files>
</install>

The new files are helper.php in the main directory and default.php and index.html created in tmpl a subdirectory of the main directory.

Before we move on you need to create the tmpl subfolder in the project - again using Eclipse (File,New,Folder). You can also create the blank html files in the main and subdirectory.

The code

Now that we have a helper and a template the main PHP file has to be modified to make use of them. The main PHP file starts off in the usual way:

<?php
defined( '_JEXEC' ) or
 die( 'Restricted access' );

just to make sure that the program can't be run in standalone mode.

Next we load the helper and call a method on the helper class:

require_once( 
dirname(__FILE__).DS.'helper.php' );
$hello = mymoduleHelper::getHello( );

Notice that the path to the helper file is constructed so as to load the file we intended i.e. helper.php but you could load any php file you want to at this stage. Equally the  class that is created in the helper file can be called whatever you want and the same for its method.

The final step is to load and run the template code within the context of the main PHP file:

require( JModuleHelper::getLayoutPath
 ( 'mod_mymodule' ) );
?>

Notice the use of the getLayoutPath method of the JModuleHelper object. This simply retrieves the default path to the template PHP file, i.e. tmpl\default.php in this case.

Banner

Notice that if the template PHP file simply contains a list PHP instructions, i.e. if it doesn't create a new object, then it shares the execution context with the code in the main PHP file and hence there is no need to actually pass any data between them.

Contrast this with the approach to creating the helper PHP file and object.

The template code, i.e. in tmpl\default.php, simply uses the $hello variable to create some HTML output:

<?php
defined( '_JEXEC' ) or
die( 'Restricted access' );
echo $hello;
?>
<ASIN:0470438533>
<ASIN:1430216425>
<ASIN:0137012314>


Last Updated ( Wednesday, 03 March 2010 )