2

I'm creating a custom module for Joomla 4, but having problems with namespacing. I've been following the steps here: https://docs.joomla.org/Special:MyLanguage/J4.x:Creating_a_Simple_Module#Namespaces but still having issues.

The module is "mod_jpc_menu_link_display" and all files are in that folder.

The helper file (/helper/mod_jpc_menu_link_display_helper.php) contains:

namespace Joomla\Module\JpcMenuLinkDisplay\Site\Helper;

as well as a class:

class MenuLinkHelper {}

The main module file (mod_jpc_menu_link_display.php) contains:

use Joomla\Module\JpcMenuLinkDisplay\Site\Helper\MenuLinkHelper;
$menuItems = MenuLinkHelper::getItems($params);

And the manifest file (mod_jpc_menu_link_display.xml contains:

<namespace>Joomla\Module\JpcMenuLinkDisplay</namespace>
<files>
    <filename module="mod_jpc_menu_link_display">mod_jpc_menu_link_display.php</filename>
    <filename>index.html</filename>
    <filename>readme.md</filename>
    <folder>tmpl</folder>
    <folder>helper</folder>
    <filename>mod_jpc_menu_link_display.xml</filename>
</files>

Everything appears proper to me; however, when the module is placed and the front-end is displayed I get a Symfony\Component\ErrorHandler\Error: ClassNotFoundError

Attempted to load class "MenuLinkHelper" from namespace "Joomla\Module\JpcMenuLinkDisplay\Site\Helper".
Did you forget a "use" statement for another namespace?

I've looked this over a bunch of times and I'm not able to find the problem with this.

Any idea why it is not loading the namespace and class properly or giving that error?

1 Answer 1

2

Joomla uses PSR-4 autoloader. You must follow this convention for builtin autoloading to work. In your case you have to rename the helper file to match the class file, i.e. it should be /Helper/MenuLinkHelper.php.

2
  • Thanks. I've seen no place (esp. in the Joomla docs) that explains that requirement. Thanks for clearing it up. Works perfectly after making that change. May 2, 2023 at 15:49
  • For documentation on Joomla4-development, I like browsing dionysopoulos.me/book.html May 3, 2023 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.