0

I've upgraded a Joomla 3.10 website to 4.x. I use custom scripts on the website wich use the framework and that worked fine until after the upgrade.

I did some searching and since the framework got an update i updated the framework and changed the script to reflect the changes. I downloaded the new framework from GitHub and installed it with composer.

However, i'm just getting a blank page. The code for the script is:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);


define('_JEXEC', 1);
define('JPATH_BASE', __DIR__);

require_once '/home/dfk/domains/dfk.be/public_html/libraries/src/Factory.php';
require_once '/home/dfk/domains/dfk.be/public_html/libraries/src/User/User.php';

// Initialize the application
$app = JFactory::getApplication('site');
$app->initialise();

use Joomla\CMS\Factory;
use Joomla\CMS\User\User;

// Initialize the application
$app = Factory::getApplication('site');
$app->initialise();

$user = User::getInstance();
$userID = $user->id;
$username = $user->username;

echo "UserID:" . $userID;
?>

I get no output at all, not even the "UserID:" string. No errors, so very hard to troubleshoot.

I've googled and tried some suggestions and nothing worked. Also tried some ChatGPT suggestions ;), but that didn't help either.

Is there anyone who has any tips on what could be wrong? Thanks in advance.

EDIT:

With the following code that i found in article Acess Joomla framework from external php file i can retrieve the session data, but i do not know how to use user data with this. Can anyone help me out with this please. There is very little documentation on the current framework, and most is outdated.

<?php

if (!defined('_JEXEC')) define('_JEXEC', 1);
if (!defined('JPATH_BASE')) define('JPATH_BASE', '/home/dfk/domains/dfk.be/public_html/');

require_once '/home/dfk/domains/dfk.be/public_html/includes/defines.php';
require_once '/home/dfk/domains/dfk.be/public_html/includes/framework.php';

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

/*
 * Alias the session service keys to the web session service as that is the primary session backend for this application
 *
 * In addition to aliasing "common" service keys, we also create aliases for the PHP classes to ensure autowiring objects
 * is supported.  This includes aliases for aliased class names, and the keys for aliased class names should be considered
 * deprecated to be removed when the class name alias is removed as well.
 */
$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.
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);

// Set the application as global app
\Joomla\CMS\Factory::$application = $app;

var_dump($app);
3
  • I've found this article and i get some session output with that, but i cannot get the userdata out fo it with this. joomla.stackexchange.com/questions/32641/…
    – Martin Law
    Commented Jun 21, 2023 at 21:40
  • If you have updated your code and managed to get the Joomla 4 to load but now have an issue with getting the userdata can you please edit your original question with the code you are now using and change the text to reflect what your new question. Or delete this question and ask a new one relating to why you can't get the userdata.
    – Irata
    Commented Jun 21, 2023 at 22:17
  • Hi @Irata , my apologies, i thought refering to the article was sufficient. I've updated my question. I left in the original code for future reference.
    – Martin Law
    Commented Jun 22, 2023 at 8:52

2 Answers 2

1

Your update coded now provides you with an instance of Joomla and you simply need to get the User object for the current user so you are almost there. There are a few options available to you but this is probably the easiest for your code.

$user = $app->getIdentity();
$id   = $user->id;
$name = $user->name;

Edit: 24/06/2023

Since I originally answered this question I have learnt more and posted a more complete answer in this post here with sample code for calling an instance of Joomla 4.

How to create an instance of the Joomla CMS from the browser or the command line

Previous sample code and misleading comments have been removed.

2
  • This worked after some little tweaks, allthoug i had to let the session block in. I will add the full working code as a seperate answer for future reference, but your assistance was vital for fixing this, thanks!
    – Martin Law
    Commented Jun 22, 2023 at 14:09
  • I have updated my answer and provided a link to a more complete understanding and sample code that actually works.
    – Irata
    Commented Jun 23, 2023 at 22:59
0

With the help from @Irata and ChatGPT and allot of trying, i was able to get to a working solution.

I had to leave the session code in for this to work, if anyone finds a simpler solution please let me know. Thanks again to Irata for pointing me in the right direction.

If this was documented better by Joomla with some examples this would have saved me 6 hours :), so i hope i can help someone else with this.

session data is now allso handled by the framework, so a session_start(); is no longer needed in your custom scripts. You will get a HTTP 500 error if you leave it in.

This is the full working code:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

if (!defined('_JEXEC')) define('_JEXEC', 1);
if (!defined('JPATH_BASE')) define('JPATH_BASE', '/home/dfk/domains/dfk.be/public_html/');

require_once '/home/dfk/domains/dfk.be/public_html/includes/defines.php';
require_once '/home/dfk/domains/dfk.be/public_html/includes/framework.php';

// Boot the DI container
$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');

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

// Get the user state service
$userState = $container->get('session')->get('user');

// Retrieve user data
$userID = $userState->get('id');
$username = $userState->get('username');

// Set the application as global app
\Joomla\CMS\Factory::$application = $app;

var_dump($userState);

echo "user name: " . $username;

?>

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.