1

I am trying to move from Joomla 3.10.11 to Joomla 4.2.2.

I have some code on custom file which contain code to delete data from database.

It was working well on Joomla 3, but on Joomla 4 it gives me an error.

$app = JFactory::getApplication('administrator');

And it gives error:

Failed to start application

enter image description here

My custom file path is:

mydomain.local/administrator/deleteRecord.php

My Code Example is

<?php
require_once(JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'framework.php');
require_once(JPATH_BASE . DS . 'includes' . DS . 'app.php');
require_once(JPATH_BASE . DS . 'libraries' . DS . 'src' . DS . 'Factory.php');
/*
    
     Code For make changes in database data.
    
    */
$app = JFactory::getApplication(); /*It gives error on this line,and breaks further process.*/
if (isset($_REQUEST['pageId']) && $_REQUEST['pageId'] == 1) {
    $app->redirect(JRoute::_('index.php?option=com_course&view=marketinglists', false), JText::_('Datensatz streichen erfolgreich.'));
} else {
    $app->redirect(JRoute::_('index.php?option=com_course&view=courselist&layout=edit&id=' . $id . ' ', false), JText::_('Datensatz streichen erfolgreich.'));
}
?>
2
  • Can you provide some more sample code around the '$app = JFactory.....' in your custom code so we can see what you are already doing, the more understanding of your code requirements the better the assistance can be. Your original question says "... 'contain code to delete data from database" however in your reply to my Answer you say " I need admin application just to redirect my website from custom file to admin component page." so it is not really clear what is happening, and it may just be a terminology thing, but some more code might help.
    – Irata
    Oct 3, 2022 at 22:55
  • @Irata, I edited my question. Oct 4, 2022 at 5:38

1 Answer 1

1

From the J4 potential backward compatibility page, https://docs.joomla.org/Potential_backward_compatibility_issues_in_Joomla_4

Factory[edit]

Factory::getApplication no longer takes arguments. These were misleading as it always returned the active application after the

first call in the bootstrap, whatever arguments were passed into the function.

I think you should be able to use $this->app which returns the active application, however there is no context around your $app = JFactory::getApplication('administrator'); to know if that is exactly what you need in your extension.

Alternatively this answer has some useful replies that might be of use to you, Get site application via Factory::getApplication() in Joomla 4 that you might adapt to for the Administrator application.

Updated 5th October....

Looking at your example code, the Joomla 4 code that I see shared around on JSE and the Joomla Issue tracker that you probably should be using to set up your environment is this.

define('_JEXEC', 1);
define('JPATH_BASE', __DIR__ . '/../../..');
require JPATH_BASE . '/includes/defines.php';
require JPATH_BASE . '/includes/framework.php';

// Boot the DI container.
$container = \Joomla\CMS\Factory::getContainer();

// Alias the session service key to the web session service.
$container->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');

// Get the application.
$app      = $container->get(\Joomla\CMS\Application\AdministratorApplication::class);

The last line essentially creates the Administrator environment for you and from there I think your additional lines of code should work for you or at least move you onto your next issue.

3
  • Same result for Factory::getApplication()->isClient('site'); , Factory::getApplication(); I need admin application just to redirect my website from custom file to admin component page. Oct 3, 2022 at 11:54
  • @VishalPatel I have update my answer with alternative code you might be able to use.
    – Irata
    Oct 4, 2022 at 22:20
  • Thanks you for your help. It worked with minor changes. Oct 6, 2022 at 7:00

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.