Joomla 4.x Tutorial - Extension Development - Modules - Namespace and Helper

Modify this post

We add namespace and helper.

For impatient people

View the changed program code in the Diff and incorporate these changes into your development version.

Step by step

In the following overview, the newly added files are marked with a background and the changed ones are outlined.

Overview of the files edited in this chapter

New files


The logic in the module may be complex. Therefore it is good to structure the code clearly. This is done by jnnhelper files. We create these in the directory Helper.

modules/mod_foo/ Helper/FooHelper.php

I named the file FooHelper in general. Good style is to give it a speaking name. Each helper file has a specific task and it should be named after it. For example, the file that loads the latest articles is called ArticlesLatestHelper. This way you can see at first sight what is in the file.

To access the file easily, we add the namespace namespace FooNamespace\Module\Foo\Site\Helper;.

modules/mod_foo/ Helper/FooHelper.php


 * @package     Joomla.Site
 * @subpackage  mod_foo
 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt

namespace FooNamespace\Module\Foo\Site\Helper;

\defined('_JEXEC') or die;

 * Helper for mod_foo
 * @since  __BUMP_VERSION__
class FooHelper
	 * Retrieve foo test
	 * @param   Registry        $params  The module parameters
	 * @param   CMSApplication  $app     The application
	 * @return  array
	public static function getText()
		return 'FooHelpertest';

Modified files

To use the contents of FooHelper in the mod_foo.php entry point, we import them using use FooNamespace\Module\Foo\Site\Helper\FooHelper;. Then we call the function FooHelper::getText() and store the result in the variable $test.

modules/mod_foo/ mod_foo.php
 \defined('_JEXEC') or die;

 use Joomla\CMS\Helper\ModuleHelper;
use FooNamespace\Module\Foo\Site\Helper\FooHelper;

$test  = FooHelper::getText();

 require ModuleHelper::getLayoutPath('mod_foo', $params->get('layout', 'default'));
modules/mod_foo/ mod_foo.xml

We enter the namespace in the manifest. This way it will be registered in Joomla during the installation. We also add the new directory so that it is copied to the right place during installation.

 	<license>GNU General Public License version 2 or later; see LICENSE.txt</license>

 		<filename module="mod_foo">mod_foo.php</filename>

In the layout, we finally access the variable. The logic for calculating the variable value is encapsulated. This keeps the layout clear. We only insert the text $test here. If we want to know more about what is behind $test, we look in the helper.

\defined('_JEXEC') or die;

echo '[PROJECT_NAME]';
echo '[PROJECT_NAME]' . $test;

Test your Joomla module

  1. install the module in Joomla version 4 to test it:

Copy the files in the modules folder into the modules folder of your Joomla 4 installation.

Install your module as described in part one, after copying all files. Joomla will update the namespaces for you during the installation. Since a file and namespaces have been added, this is necessary.

  1. Check whether the text calculated via the function FooHelper::getText() is displayed in the frontend.


Joomla Dokumentation

Modify this post