4

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

<?php
// 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)
var_dump($templateParams);

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:

Symfony\Component\ErrorHandler\Error\ClassNotFoundError:
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}()
     (/srv/http/libraries/vendor/joomla/di/src/ContainerResource.php:182)
  at Joomla\DI\ContainerResource->getInstance()
     (/srv/http/libraries/vendor/joomla/di/src/Container.php:96)
  at Joomla\DI\Container->get()
     (/srv/http/libraries/src/Extension/ExtensionManagerTrait.php:174)
  at Joomla\CMS\Application\CMSApplication->loadExtension()
     (/srv/http/libraries/src/Extension/ExtensionManagerTrait.php:50)
  at Joomla\CMS\Application\CMSApplication->bootComponent()
     (/srv/http/libraries/src/Application/SiteApplication.php:442)
  at Joomla\CMS\Application\SiteApplication->getTemplate()
     (/srv/http/test.php:23)

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.

4
  • 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

0

Your Answer

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