Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create a simple custom module in Joomla?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 386
    Comment on it

    Hello friends, today we will discuss how to create a custom module in joomla. Here, we are creating a module example. You can change the name of your according to your requirement.

    To create a module in joomla four basic files are required:

    1. mod_example.php:

    mod_example.php is basically the module's entry point. It includes all the necessary data and output that you want to display in the module.

    It includes the following steps:

    •  You need to include the helper.php file. For ex:
    require_once dirname(__FILE__) . '/helper.php'
    •  You need to call the required helper class method to fetch the data.
    • Last, you need to include the template

    Code for mod_example.php is as follows:

    <?php
    /**
     *Example Module Entry Point
     *
     */
     
    // No direct access
    defined('_JEXEC') or die;
    // Include the syndicate functions only once
    require_once dirname(__FILE__) . '/helper.php'
     
    $example = modExampleHelper::getData($params);
    require JModuleHelper::getLayoutPath('mod_example');
    
    

    2. mod_example.xml:
    mod_example.xml contains all the details related to the module i.e., specify the files or the directory that are the part of the module. Below we are giving the code for mod_example.php:

    <?xml version="1.0" encoding="utf-8"?>
    <extension type="module" version="3.1.0" client="site" method="upgrade">
        <name>Example</name>
        <author>Author Name</author>
        <version>1.0.0</version>
        <description>An example module.</description>
        <files>
            <filename>mod_example.xml</filename>
            <filename module="mod_example">mod_example.php</filename>
            <filename>index.html</filename>
            <filename>helper.php</filename>
            <filename>tmpl/default.php</filename>
            <filename>tmpl/index.html</filename>
        </files>
        <config>
        </config>
    </extension>

    Here you have noticed that there are two file that we haven't mentioned in the discussion i.e, index.html and tmpl/index.html. These files are used so that the user cannot browse the directory. If user directly points the directory it will open the index.html. You can left these files empty or simply add the following code:

    <html><body bgcolor="#FFFFFF"></body></html>

    3. helper.php:
    helper.php is used to fetch the data that you want to display display in the module from database or other sources.

    Code for helper.php is as follows:

    <?php
    /**
     * Helper class for Example module
     * 
     */
    class ModExampleHelper
    {
        /**
         * Retrieves the example message
         *
         * @param   array  $params An object containing the module parameters
         *
         * @access public
         */    
        public static function getData($params)
        {
            return 'Example Module';
        }
    }

    4. tmpl/default.php:


    It's the template of the module. This file gets the data from the mod_helloworld.php and display the HTML for the module.

    Code for default.php is as follows:

    <?php 
    // No direct access
    defined('_JEXEC') or die; ?>
    <?php echo $example; ?>

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: