1

I am trying to set up a set of common code, for this example traits, in \libraries\vendor\mystuff under Joomla 4 using namespace'd code and I haven't been able to find a similar looking example or current documentation. I have done similar with Joomla 3 that still works with Joomla 4 but I would like make it right for Joomla 4 and beyond.

My first question is do I still need to create a plugin to load the libraries like Joomla 3 if I am using namespaces? e.g.

public function onAfterInitialise()
{
    JLoader::registerNamespace(....);
}  

I vaguely recall reading something that suggests the answer is yes, however I can't see other extensions doing that so I am hoping auto-loading/namespaces is going to make that extra step redundant.

This is my attempt so far, unfortunately it cant find my Trait.

Attempted to load trait "DatalayerTrait" from namespace "Bricks\Datalayer". Did you forget a "use" statement for another namespace?

In the manifest file, administrator/manifests/libraries/bricks.xml, I have this;

     ...
<libraryname>bricks</libraryname>
    ...
<namespace path="src">Bricks\Datalayer</namespace>
        
<files folder="vendor\bricks">
    <folder>datalayer</folder>
</files>
        ....

In libraries/vendor/bricks/datalayer/src/DatalayerTrait.php I have my Trait;

<?php

namespace Bricks\Datalayer;

trait DatalayerTrait
{
...
}

In my class where I want to use the Trait I have;

...
use Bricks\Datalayer\DatalayerTrait;
...
class ....
{
    use DatalayerTrait;
...
}

Any pointers to relevant documentation or example(s) of how it should be done would be appreciated.

1 Answer 1

1

It is quite straightforward in the end, but like many things in Joomla not well documented if at all. Joomla 4 makes it easier as the Library is available to the autoloader so there is no longer any need to create a separate plugin to register or load your own library.

In your manifest(.xml) file for the library you need to supply these two lines that will be used to create the entry in autoload_psr4.php

<libraryname>bricks</libraryname>
<namespace path="src">Bricks\Library</namespace>

The value between the <libraryname></libraryname>, in this case bricks, is prefixed to the value in the path="src" and as it is installed as a Library extension it gets the JPATH_LIBRARIES prefixed to match the directory where the code is copied to. The code can be found in /libraries/bricks/src

The value between the <namespace></namespace>, in this case Bricks\Library, takes pride of place on the left side of the autoloader entry and is the namespace prefix used.

'Bricks\\Library\\' => [JPATH_LIBRARIES . '/bricks/src'],

The namespace in the top of your files is then namespace Bricks\Library; and the Use statement looks something like this, use Bricks\Library\DatalayerTrait;

1
  • Thank you to @pe7er as I followed his approach for the regularlabs library to get to the above answer.
    – Irata
    Oct 27, 2022 at 7:08

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.