joomla-json-db-check
joomla-json-db-check copied to clipboard
"Failed to start application" on Joomla 4
When trying to use the script after upgrade from Joomla 3.10 to 4.40, it failed with
Exception: Failed to start application
Error occured in line 64:
$app = JFactory::getApplication('site');
The exception has been thrown here:
<joomla_dir>/libraries/src/Factory.php:158
* @throws \Exception
*/
public static function getApplication()
{
if (!self::$application) {
throw new \Exception('Failed to start application', 500);
}
return self::$application;
}
Can it be that the way of starting scripts in Joomla has changed somewhere between the versions?
I was able to fix the issue by applying the approach taken here, also considering this post.
After replacing the lines 63-64 with following code, script worked as expected:
// Instantiate the application.
$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);
This is of course a "workaround" only, I am not a developer and am not sure about the appropriateness of the code, except that it worked ;).
Can this be addressed / incorporated into some fix?
The script solved my problem by the way, thank you very much! :)