1

I want to autoload multiple helpers in my custom component.

In all the docs I've found, the examples for using JRegisterNamespace show the classes being loaded from the Joomla /libraries as the root.

Is this the only use of the namespace loader? Or could I register (for example) my component's helpers directory and load all my helpers automatically instead of using a bunch of require statements?

If possible, please provide example of how this would be done.

Thanks!

0

There are a couple of ways you could approach this.

The first is the put all your custom files in the helpers folder. If you opted for this approach I'd recommend providing a single entry point which either loads all required class files or provides programmatic access to the underlying functionality. Then simply include the file using JLoader:

JLoaders::register('MyCustomHelpers', JPATH_COMPONENT . '/helpers/mycustomhelpers.php');

The other methods involve using the root libraries folder. Although you could register the classes the same way as with files in the helpers folder of your component, Joomla provides a better mechanism for loading files in the libraries folder. For example, let's say you created a folder in libraries called 'acme' which had a folder called 'helpers' with a PHP class called widgets.php. You would load this file from anywhere inside your code base by typing:

jimport('acme.helpers.widgets');

This way still requires manual addition where ever the class is the be used, and of course managing all the dependencies inside the acme folder (which is the same as managing files in the helpers folder).

If you have a complex set of helpers classes or custom libraries a much better, more comprehensive solution would be to use namespaces. A full discussion on namespacing is beyond the scope of this answer, but to include a namespace requires proper formating of folder taxonomy and file naming; as well executing the JLoader::regsiterNamspace() command during the onAfterInitialise() event.

Autoload: namespaces require autoload files, which can be generic or specific to your naming convention. I would suggest using a generic one as it will force development to adhere to the standards. A simple file called autoload.php loaded at libaries/acme/src/Acme/autoload.php with the following should work:

function acmeAutoload ($className)
{
    include(__DIR__ . "/" . $className . ".php");
}

spl_autoload_register("acmeAutoload");

Folders: All namespaced files must be under a src folder. So using previous examples, your taxonomy would be:

libraries/acme/src/Acme/Helpers/Widgets.php

Register: Create a plugin or added a hook into an existing plugin for the onAfterInitialise event like so:

public function onAfterInitialise()
{
    JLoader::registerNamespace('Acme', JPATH_LIBRARIES . '/acme/src');
}

Here are a couple of links which helped me along the way. Hope this helps!

Joomla Namespacing

PSR-0

PHP Namespacing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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