Advertisement

old script failing in joomla 4 - Factory:: issue Topic is solved

For Joomla! 4.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
ahollan1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Nov 21, 2024 10:55 pm

old script failing in joomla 4 - Factory:: issue

Post by ahollan1 » Thu Nov 21, 2024 11:22 pm

web site was just updated from Joomla 3.x to Joomla 4.x - PHP 8.1
scripts that used to work after member login are now failing when script reaches statements using Factor:: functions.
original [inherited] script still used JFactory. After some changes based on articles found online, the script still fails.

1) manually inserting debug statements and selective commenting out Factory function calls all point to problems with the lines using Factory::
2) Factory.php message is "Failed to start application"
3) if all Factory functions commented out - enduser error stating "you must be logged in"

here's the code with original lines commented out and new lines identified as // attempt after joomla4 migration

***************************************** BEGIN CODE **********************************************************************************
// Get Joomla session
use Joomla\CMS\Factory; // attempt after joomla4 migration
define( '_JEXEC', 1 );
define( 'DS', '/' );

// IMPORTANT: adjust path based on folder or define it manually as string
// "myjoomlaroot" is name of your Joomla root folder

// $myjoomlaroot = (strpos($_SERVER["REQUEST_URI"],"") > 0) ? "" : "";

// define( 'JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] . $myjoomlaroot );
define( 'JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] );


// require_once ( JPATH_BASE .DS. 'includes' .DS. 'defines.php' ); // comment out for joomla4
// require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' ); // comment out for joomla4
require_once ( JPATH_BASE .'/includes/defines.php' ); // attempt after joomla4 migration
require_once ( JPATH_BASE .'/includes/framework.php' ); // attempt after joomla4 migration

// $mainframe = JFactory::getApplication('site'); // comment out for joomla4
// $app = JFactory::getApplication(); // comment out for joomla4
$mainframe = Factory::getApplication(); // attempt after joomla4 migration
$app = Factory::getApplication(); // attempt after joomla4 migration
$prefix = $app->get('dbprefix');
$mainframe->initialise();
jimport( 'joomla.user.user');
jimport( 'joomla.session.session');
jimport( 'joomla.user.authentication');
// get the database infor mation
// $appvars = JFactory::getApplication(); // connect will use this
$appvars = Factory::getApplication(); // connect will use this // attempt after joomla4 migration
// now get user object and 3 example user variables
//$user = JFactory::getUser();
$user = Factory::getUser(); // attempt after joomla4 migration

***************************************** END CODE **********************************************************************************

Advertisement
User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 31392
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: old script failing in joomla 4 - Factory:: issue

Post by Per Yngve Berg » Fri Nov 22, 2024 5:17 am

The new named space function was introduced in 3.8.0

https://api.joomla.org/cms-4/classes/Jo ... ctory.html

I don't see you have any use statements that say what containers to use.

ahollan1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Nov 21, 2024 10:55 pm

Re: old script failing in joomla 4 - Factory:: issue

Post by ahollan1 » Fri Nov 22, 2024 3:34 pm

not sure what you think is misssing

isn't the namespace defined by line 2:
use Joomla\CMS\Factory;

confess that i don't know the term "containers"

is it possible to show an example if this means that the syntax of the function calls is missing something?
investigation old syntax of $mainframe = JFactory::getApplication('site') led me to both JFactory change to Factory and removal of qualifying 'site'.

confession #2 - i don't know how to determine which data fields are available for either the getApplication() or getUser() functions.

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 44748
Joined: Sat Apr 05, 2008 9:58 pm

Re: old script failing in joomla 4 - Factory:: issue

Post by Webdongle » Sat Nov 23, 2024 4:59 am

http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"When I'm right no one remembers but when I'm wrong no one forgets".

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17904
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: old script failing in joomla 4 - Factory:: issue

Post by toivo » Sat Nov 23, 2024 6:25 am

The following code works in both CLI and browser mode from a subfolder in the main Joomla folder:

Code: Select all

<?php
define('_JEXEC', 1);
use Joomla\CMS\Factory;
error_reporting(E_ALL);
ini_set('display_errors', 1);

// this script is run from subfolder of Joomla folder
define('JPATH_BASE', realpath(dirname(__FILE__) . '/..'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

// Boot the DI container
$container = Factory::getContainer();

/*
 * Alias the session service keys to the web session service as that is the primary session backend for this application
 */
$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');

// instantiate the application and set as global app
$app 	= $container->get(\Joomla\CMS\Application\SiteApplication::class);
Factory::$application = $app;

// cross platform compatible line break
if(php_sapi_name() == "cli") {
	$crlf	= PHP_EOL;
} else {
	$crlf = '<br>';
}

$user	= $app->getSession()->get('user');
if ($user->guest) {
	echo 'this is a guest user' . $crlf;
} else {
	echo 'user id = ' . $user->id . ', name = ' . $user->name . $crlf;
}

$db	= Factory::getDbo();
$q      = $db->getQuery(true)
   ->select('COUNT(*)')
   ->from('#__users');
$db->setQuery($q);
$count   = $db->loadResult();
echo $count . ' users in _users table';
Toivo Talikka, Global Moderator

ahollan1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Nov 21, 2024 10:55 pm

Re: old script failing in joomla 4 - Factory:: issue

Post by ahollan1 » Sun Nov 24, 2024 3:09 am

i tried implementing the code from toivo. problems popped up in multiple places. i don't understand what the script is trying to do, how the code is supposed to work or how to track down/correct the problems.

1) define('JPATH_BASE', realpath(dirname(__FILE__) . '/..'));
failed immediately by not being able to find anything
i restored define( 'JPATH_BASE', $_SERVER['DOCUMENT_ROOT'] ); and was able to progress a littl

2) $app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
Warning: session_name(): Session name cannot be changed after headers have already been sent in /home/usbf/public_html/libraries/vendor/joomla/session/src/Storage/NativeStorage.php on line 405

i tried reading NativeStorage.php but couldn't backtrack to identify what was trying to reset session name

3) $user = $app->getSession()->get('user');
runtime exception
Failed to start the session because headers have already been sent by "/home/usbf/public_html/apps/includes/session.php" at line 33.

line 33 in my script is just echoing some information about which command is about to or was just executed.
it exists between the command to define JPATH_BASE and require_once defines.php

trace is:

RuntimeException:
Failed to start the session because headers have already been sent by "/home/usbf/public_html/apps/includes/session.php" at line 33.

at /home/usbf/public_html/libraries/vendor/joomla/session/src/Storage/NativeStorage.php:473
at Joomla\Session\Storage\NativeStorage->start()
(/home/usbf/public_html/libraries/src/Session/Storage/JoomlaStorage.php:295)
at Joomla\CMS\Session\Storage\JoomlaStorage->start()
(/home/usbf/public_html/libraries/vendor/joomla/session/src/Session.php:406)
at Joomla\Session\Session->start()
(/home/usbf/public_html/libraries/vendor/joomla/session/src/Session.php:333)
at Joomla\Session\Session->has('user')
(/home/usbf/public_html/libraries/src/Session/Session.php:194)
at Joomla\CMS\Session\Session->get('user')
(/home/usbf/public_html/apps/includes/session.php:92)


i'm lost

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17904
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: old script failing in joomla 4 - Factory:: issue

Post by toivo » Sun Nov 24, 2024 3:24 am

Put the script into a subfolder of the main Joomla folder, as mentioned in my post and the comment line in the code.

The purpose of the script is to show how the Factory methods work after the container has been created. The script displays the current user, if a user is logged in, and displays the number of users in the users table, just as an example.
Toivo Talikka, Global Moderator

ahollan1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Nov 21, 2024 10:55 pm

Re: old script failing in joomla 4 - Factory:: issue

Post by ahollan1 » Sun Nov 24, 2024 8:25 pm

I didn't see the original qualifier of you having placed the script in the joomla folder structure.
Or maybe subconscious rejection because I don't love the idea of placing a script that we own in a joomla-owned folder structure, but i'll give it a try.

ahollan1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Nov 21, 2024 10:55 pm

Re: old script failing in joomla 4 - Factory:: issue

Post by ahollan1 » Mon Nov 25, 2024 12:42 am

relocating the script eliminated problem #1
problems #2 and #3 persist

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17904
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: old script failing in joomla 4 - Factory:: issue

Post by toivo » Mon Nov 25, 2024 2:47 am

If you copied and pasted the script, it should work. It was tested locally in Wampserver using Joomla 4.4.9 and PHP 8.1.13, locally in Joomla 5.2.1 and PHP 8.3.14 and remotely in Joomla 5.2.1 and PHP 8.2.25.
Toivo Talikka, Global Moderator

ahollan1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Thu Nov 21, 2024 10:55 pm

Re: old script failing in joomla 4 - Factory:: issue

Post by ahollan1 » Mon Nov 25, 2024 1:52 pm

yes - pure copy.

had not tested before, but just ran the script directly and it did work.

once direct running of script was successful, i looked at the parent script.
turns out the problem was embedding the include statement inside an if(file_exists()) statement.
i added that to the parent to make sure define('JPATH_BASE'... found the relocated script.

got rid of that paranoid check and things seem to be working!!!
thanks so much for your patient help

now i just need to attack the warning and deprecation statements associated with upgrade from PHP 8.0.30 to 8.1.30. That activity IS in my comfort zone. [Yes, I am trying to get PHP upgraded to 8.3]

thanks again

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17904
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: old script failing in joomla 4 - Factory:: issue

Post by toivo » Mon Nov 25, 2024 9:05 pm

Cheers, happy to have been able to help!
Toivo Talikka, Global Moderator

Advertisement

Post Reply

Return to “Joomla! 4.x Coding”