I have a file test.php that manually loads the Joomla framework and prints template parameters.

// test.php, located in the same directory that index.php is
define('_JEXEC', 1);
define('JPATH_BASE', '.');

require_once(JPATH_BASE . '/includes/defines.php');
require_once(JPATH_BASE . '/includes/framework.php');

$container = \Joomla\CMS\Factory::getContainer();

$container->alias('session.web', 'session.web.site')
    ->alias('session', 'session.web.site')
    ->alias('JSession', 'session.web.site')
    ->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
    ->alias(\Joomla\Session\Session::class, 'session.web.site')
    ->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);

\Joomla\CMS\Factory::$application = $app;

// Uncomment to mitigate the error:
// JLoader::registerNamespace('Joomla\Component\Templates\Administrator', JPATH_ADMINISTRATOR . '/components/com_templates/src');
$templateParams = $app->getTemplate(true)->params;// error (see explanation below)

In Joomla 4.1, the call to app->getTemplate(true) works fine because the function makes the database calls itself.

In Joomla 4.2, the call to app->getTemplate(true) delegates the database calls to the com_templates service provider (I'm not exactly sure how). But since the Joomla\Component\Templates\Administrator\Extensions namespace isn't loaded, we get a ClassNotFoundError for class TemplatesComponent.

Below is the stack trace:

Attempted to load class "TemplatesComponent" from namespace "Joomla\Component\Templates\Administrator\Extension".
Did you forget a "use" statement for another namespace?

  at /srv/http/administrator/components/com_templates/services/provider.php:47
  at Joomla\DI\ServiceProviderInterface@anonymous/srv/http/administrator/components/com_templates/services/provider.php:28$484->{closure}()
  at Joomla\DI\ContainerResource->getInstance()
  at Joomla\DI\Container->get()
  at Joomla\CMS\Application\CMSApplication->loadExtension()
  at Joomla\CMS\Application\CMSApplication->bootComponent()
  at Joomla\CMS\Application\SiteApplication->getTemplate()

If I add $app->getTemplate(true) to an HtmlView::display() function, it works just fine and a var_dump shows what I'd expect.

So I was wondering if I was initializing the Joomla framework wrong or something.

  • 1
    I had the same problem after updating from J4.1.0 to J4.2.0 and your post made me resolve the issue. Adding the 'JLoader::registerNamespace...' line did the trick. Thank you!
    – RobHU
    Jan 20, 2023 at 11:48
  • Hm which HtmlView::display function did you change exactly? Jun 15, 2023 at 10:30
  • Hm my error seems more related to the autoload_psr4.php file, and probably write permission confusion. I could copy it from another machine. Jun 15, 2023 at 10:41
  • Came back to this, the precise file location is administrator/cache/autoload_psr4.php Oct 17, 2023 at 13:09


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.