2

I have created a very basic module to display EasyPost Tracking information for our customers. The module works in J3, but in J4 I get an error.

This is the code I have:

use Joomla\CMS\Factory;
$input = Factory::getApplication()->input;
$tracking_number = $input->get("tracking_number", "", "ALNUM");
$carrier = $input->get("carrier", "", "ALNUM");
$api_key = $params->get('api_key');

// add EasyPost NameSpace library
JLoader::registerNamespace('EasyPost', JPATH_LIBRARIES. '/lib');
// get EasyPost library
require_once(JPATH_LIBRARIES . '/lib/EasyPost/EasyPost.php');
// set api key
\EasyPost\EasyPost::setApiKey($api_key);

if(!empty($tracking_number)) {
    try {
        $EZtracker = \EasyPost\Tracker::create(array(
            "tracking_code" => $tracking_number,
            "carrier" => $carrier
        ));

When I call the class to set the api key it seems to work.

\EasyPost\EasyPost::setApiKey($api_key);

But when I call the static create method from tracker class with:

$EZtracker = \EasyPost\Tracker::create(array(...));

I get the error:

Message: Class "EasyPost\Tracker" not found.

How do I update this code to work in Joomla 4?

3
  • There is not enough detail in your original question to show how EasyPost library has been installed in your Joomla 4 environment to know if it is correctly adding itself to the autoload function of Joomla 4 via the <namespace> attribute of the manifest as part of the install process of a library extension. Is the EasyPost Library installed as an extension or have you just added it to your Library directory? If you comment the out the 'require_once' statement does the setApiKey() statement also report class not found?
    – Irata
    Commented Jan 9, 2023 at 8:50
  • @Irata Thank You for looking at this. The EasyPost library was manually loaded into the directory libraries/lib/EasyPost. I have not loaded it via the library install process or added a <namespace> attribute for this library other than what is in the code above. If I comment out the 'require_once' statement the setApiKey() statement does report class not found. I do not think the class is being added to the autoload function. The entry does not show up in the chache/autoload_psr4.php file either.
    – jfischer
    Commented Jan 10, 2023 at 2:59
  • I think you will need to add a require_once statement for the Tracker.php similar to the line you commented out to make it work.
    – Irata
    Commented Jan 10, 2023 at 6:14

1 Answer 1

2

Assuming you are using the same code for J3, it's working by coincidence. The library is using PSR4 autoloading but in J3 JLoader::registerNamespace() defaults to PSR0. Normally this wouldn't work but you're also registering the wrong directory which ends up coinciding with how PSR0 library would be structured. Essentially, you need to switch to PSR4 autoloading for J3 (J4 supports only PSR4) and register the correct directory to make the code work for both J3 and J4:

JLoader::registerNamespace('EasyPost', JPATH_LIBRARIES. '/lib/EasyPost', false, false, 'psr4');

An alternative would be to include the lib/easypost.php file but that would be slower since it would load all class files even when they're not used. Additionally, this may stop working when the library becomes dependent on other libraries (i.e. like v6 does).

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.