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.