J3.x

Difference between revisions of "Creating a simple module/Developing a Basic Module/nl"

From Joomla! Documentation

< J3.x:Creating a simple module
(Created page with "De helper class methode wordt op de volgende manier aangeroepen:")
(Created page with "=== Voltooide mod_helloworld.php bestand ===")
Line 39: Line 39:
 
<source lang="php">$hello = modHelloWorldHelper::getHello($params);</source>
 
<source lang="php">$hello = modHelloWorldHelper::getHello($params);</source>
  
=== Completed mod_helloworld.php file ===
+
=== Voltooide mod_helloworld.php bestand ===
  
 
The complete <tt>mod_helloworld.php</tt> file is as follows:
 
The complete <tt>mod_helloworld.php</tt> file is as follows:

Revision as of 03:17, 30 June 2015

Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Nederlands • ‎español • ‎français • ‎português do Brasil • ‎русский • ‎中文(台灣)‎
Joomla! 
3.x
Handleiding
Een eenvoudige module maken

Dit is een serie artikelen over het ontwikkelen van een module voor Joomla! versie Joomla 3.x. U kunt navigeren binnen de artikelen in deze serie met behulp van het navigatie drop-down menu.

Begin met de Introductie en navigeer door de artikelen van de serie door middel van de navigatieknop onderaan of het vak rechts (Artikelen in deze serie). Er zitten 2 video's bij deze handleiding die u hier kunt zien Basic Joomla Module Development video 1 en Basic Joomla Module Development video 2.




Een module is een lichtgewicht en flexibele extensie.Ze worden gebruikt voor kleine onderdelen van de pagina die in het algemeen minder ingewikkeld zijn en die getoond kunnen worden bij verschillende componenten.

U kunt veel voorbeelden van modules zien in de standaard Joomla! installatie: - Menu's - Laatste nieuws - Inlogformulier - en veel meer.

Deze handleiding legt uit hoe een eenvoudige 'Hallo wereld' module gemaakt moet worden. Door middel van deze handleiding leert u de basis bestandsstructuur van een module. Deze basisstructuur kan dan uitgebreid worden om uitgebreidere modules te maken.

Bestandsstructuur

Er zijn vier basis-bestanden die worden gebruikt in het standaard patroon van module ontwikkeling:

  • mod_helloworld.php - Dit bestand is de hoofdingang van de module. Het voert alle nodige initialisatie routines uit, roept hulp-routines aan om noodzakelijke gegevens te verzamelen, en neemt het template op dat de module-uitvoer toont.
  • mod_helloworld.xml - Dit bestand bevat informatie over de module. Het definieert de bestanden die geïnstalleerd moeten worden door de Joomla! installer en bepaalt instellingen-parameters van de module.
  • helper.php - Dit bestand bevat de helper class die gebruikt wordt om het uiteindelijke werk uit te voeren door het ophalen van informatie die getoond moet worden in de module (meestal uit de database of een andere bron).
  • tmpl/default.php - Dit is de module template. Dit bestand pakt de gegevens op die mod_helloworld.php verzamelt en genereert de HTML die op de pagina vertoond wordt.

Het maken van mod_helloworld.php

Het mod_helloworld.php bestand voert drie taken uit:

  • neemt het helper.php bestand op dat de class bevat die gebruikt moet worden om de noodzakelijke gegevens op te halen
  • roept de juiste helper class methode aan om de gegevens op te halen
  • haalt het template op om de uitvoer te tonen.

De helper class is gedefinieerd in ons helper.php bestand. Dit bestand wordt opgenomen met een 'require_once' statement:

require_once dirname(__FILE__) . '/helper.php';

require_once wordt gebruikt omdat onze helper-functies binnen een class zijn gedefinieerd en we willen dat de class slechts één keer wordt gedefinieerd.

Onze helper class is nog niet gedefinieerd, maar als dat wel zo is, dan bevat het een methode: getHello(). Voor ons basis voorbeeld is het niet echt noodzakelijk om dit te doen - het “Hello, World” bericht dat deze methode teruggeeft kan eenvoudig opgenomen worden in het template. We gebruiken hier een helper class om de basis techniek te tonen.

Onze module gebruikt nu geen parameters, maar we geven ze toch door aan de helper methode zodat het later gebruikt kan worden als we besluiten de functionaliteit van onze module uit te breiden.

De helper class methode wordt op de volgende manier aangeroepen:

$hello = modHelloWorldHelper::getHello($params);

Voltooide mod_helloworld.php bestand

The complete mod_helloworld.php file is as follows:

<?php
/**
 * Hello World! Module Entry Point
 * 
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @license    GNU/GPL, see LICENSE.php
 * @link       http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
 * mod_helloworld is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

// No direct access
defined('_JEXEC') or die;
// Include the syndicate functions only once
require_once dirname(__FILE__) . '/helper.php';

$hello = modHelloWorldHelper::getHello($params);
require JModuleHelper::getLayoutPath('mod_helloworld');

The one line that we haven’t explained so far is the first line. This line checks to make sure that this file is being included from the Joomla! application. This is necessary to prevent variable injection and other potential security concerns.

Creating helper.php

The helper.php file contains that helper class that is used to retrieve the data to be displayed in the module output. As stated earlier, our helper class will have one method: getHello(). This method will return the ‘Hello, World’ message.

Here is the code for the helper.php file:

<?php
/**
 * Helper class for Hello World! module
 * 
 * @package    Joomla.Tutorials
 * @subpackage Modules
 * @link http://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
 * @license        GNU/GPL, see LICENSE.php
 * mod_helloworld is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */
class ModHelloWorldHelper
{
    /**
     * Retrieves the hello message
     *
     * @param   array  $params An object containing the module parameters
     *
     * @access public
     */    
    public static function getHello($params)
    {
        return 'Hello, World!';
    }
}

There is no rule stating that we must name our helper class as we have, but it is helpful to do this so that it is easily identifiable and locatable. Note that it is required to be in this format if you plan to use the com_ajax plugin.

More advanced modules might include database requests or other functionality in the helper class method.

Creating tmpl/default.php

The default.php file is the template which displays the module output.

The code for the default.php file is as follows:

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

An important point to note is that the template file has the same scope as the mod_helloworld.php file. What this means is that the variable $hello can be defined in the mod_helloworld.php file and then used in the template file without any extra declarations or function calls.

Creating mod_helloworld.xml

The mod_helloworld.xml is used to specify which files the installer needs to copy and is used by the Module Manager to determine which parameters are used to configure the module. Other information about the module is also specified in this file.

The code for mod_helloworld.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<extension type="module" version="3.1.0" client="site" method="upgrade">
    <name>Hello, World!</name>
    <author>John Doe</author>
    <version>1.0.0</version>
    <description>A simple Hello, World! module.</description>
    <files>
        <filename>mod_helloworld.xml</filename>
        <filename module="mod_helloworld">mod_helloworld.php</filename>
        <filename>index.html</filename>
        <filename>helper.php</filename>
        <filename>tmpl/default.php</filename>
        <filename>tmpl/index.html</filename>
    </files>
    <config>
    </config>
</extension>

Manifest files explains the technical details of the elements used in the XML file.

You will notice that there are two additional files that we have not yet mentioned: index.html and tmpl/index.html. These files are included so that these directories cannot be browsed. If a user attempts to point their browser to these folders, the index.html file will be displayed. These files can be left empty or can contain the simple line:

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

which will display an empty page.

Since our module does not use any form fields, the config section is empty.

Conclusion

Module development for Joomla! is a fairly simple, straightforward process. Using the techniques described in this tutorial, an endless variety of modules can be developed with little hassle.