0

I'm sure I've missed something obvious, but I'm stumped.

I have a helper class located at:
administrator/components/com_supersite/src/Helper/supersite.php

<?php

\defined('_JEXEC') or die();

class Supersite {

I'm using a system plugin to register it as:

<?php
\defined('_JEXEC') or die;

use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Log\Log;

class PlgSystemSupersite extends CMSPlugin
{

    public function onAfterInitialise() {
        Log::add('running onAfterInitialise in supersite plugin');
        JLoader::registerPrefix('Supersite', JPATH_ADMINISTRATOR . '/components/com_supersite/src');
    }
}

And then I try to do a very basic call to the class, pulling a json string to array:

<?php

namespace Joomla\Component\Supersite\Site\Controller;

use Joomla\CMS\MVC\Controller\BaseController;
use Supersite;

\defined('_JEXEC') or die;

class ajaxController extends BaseController
{

    public function json()
    {
        $supersite = new Supersite();
        
        $prices = $supersite->getData('products/customer-price.json');
        echo json_decode($prices);
    }
}

And the resulting error message:

{"error":true,"code":0,"message":"Class \"Supersite\" not found","trace":"#0 C:\\Users\\username\\OneDrive\\Documents\\Development\\xampp\\htdocs\\joomla\\libraries\\src\\MVC\\Controller\\BaseController.php(672): Joomla\\Component\\Supersite\\Site\\Controller\\ajaxController->json()\n#1 C:\\Users\\username\\OneDrive\\Documents\\Development\\xampp\\htdocs\\joomla\\libraries\\src\\Dispatcher\\ComponentDispatcher.php(143): Joomla\\CMS\\MVC\\Controller\\BaseController->execute('json')\n#2 C:\\Users\\username\\OneDrive\\Documents\\Development\\xampp\\htdocs\\joomla\\libraries\\src\\Component\\ComponentHelper.php(355): Joomla\\CMS\\Dispatcher\\ComponentDispatcher->dispatch()\n#3 C:\\Users\\username\\OneDrive\\Documents\\Development\\xampp\\htdocs\\joomla\\libraries\\src\\Application\\SiteApplication.php(200): Joomla\\CMS\\Component\\ComponentHelper::renderComponent('com_supersite')\n#4 C:\\Users\\username\\OneDrive\\Documents\\Development\\xampp\\htdocs\\joomla\\libraries\\src\\Application\\SiteApplication.php(241): Joomla\\CMS\\Application\\SiteApplication->dispatch()\n#5 C:\\Users\\username\\OneDrive\\Documents\\Development\\xampp\\htdocs\\joomla\\libraries\\src\\Application\\CMSApplication.php(294): Joomla\\CMS\\Application\\SiteApplication->doExecute()\n#6 C:\\Users\\username\\OneDrive\\Documents\\Development\\xampp\\htdocs\\joomla\\includes\\app.php(61): Joomla\\CMS\\Application\\CMSApplication->execute()\n#7 C:\\Users\\username\\OneDrive\\Documents\\Development\\xampp\\htdocs\\joomla\\index.php(32): require_once('C:\\\\Users\\\\username\\\\...')\n#8 {main}"}

1 Answer 1

3

You are using Joomla4 and Namespaces therefore you no longer need to have a plugin to register the class, that should be available to the Autoloader in /administrator/cache/autoload_psr4.php.

The namespace for your component should not use the Joomla\ prefix but one of your own, eg. James\.

In your manifest file, supersite.xml, you should have your namespace element set like this <namespace path="src">James\Component\Supersite</namespace>

In your administrator/components/com_supersite/src/Helper/supersite.php it should have a Namespace of James\Component\Supersite\Administrator\Helper.

And your Controller class should be namespaced as James\Component\Supersite\Site\Controller.

If you are updating the files on your Joomla instance directly you will need delete the file /administrator/cache/autoload_psr4.php where it will be recreated automatically with a line near the top that looks like

'James\\Component\\Supersite\\Administrator\\' => [JPATH_ADMINISTRATOR . '/components/Supersite/src'],
'James\\Component\\Supersite\\Site\\' => [JPATH_SITE . '/components/Supersite/src'],

If you have an API section of your component there will be a similar line created for it as well in the autoload file.If you are going through the Install process to bring in the above changes then it will/should automatically rebuild the autoload_psr4.php file for you and should not require being deleted.

In your Controller you need to replace use Supersite; with use James\Component\Supersite\Administrator\Helper\Supersite

If I have got it right that should get it working for you or at least a lot closer and without the use of the Plugin. The approach is sensitive to the case used in the Namespace and the files/folders they resolve to.

1
  • Great answer @Irata, thank you!
    – James
    Dec 5, 2022 at 15:12

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.