|More Advanced Joomla Modules with PHP and Eclipse|
|Wednesday, 11 November 2009|
Page 1 of 2
It's a good idea to adopt a lightweight Model View Controller (MVC) pattern making use of a helper file and a template
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"?>
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.
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:
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:
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:
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.
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:
|Last Updated ( Wednesday, 03 March 2010 )|