Skip to main content
added 6 characters in body
Source Link
Irata
  • 4.4k
  • 3
  • 9
  • 21

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 SinceEdit: 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.

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.

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.

Previous sample code and misleading comments have been removed.
Source Link
Irata
  • 4.4k
  • 3
  • 9
  • 21

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;

You can also removeEdit: 24/06/2023 Since I originally answered this block from the sample you picked up if youquestion I have no need for session data inlearnt more and posted a simple functionmore complete answer in this post here with sample code for calling an instance of Joomla 4.

/*
 * 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');

And I am not sure but I don't think this line is required either for your purposes,How to create an instance of the Joomla CMS from the browser or the command line

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

So your completePrevious sample code should now look like this;and misleading comments have been removed.

<?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();

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

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

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;

You can also remove this block from the sample you picked up if you have no need for session data in a simple function.

/*
 * 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');

And I am not sure but I don't think this line is required either for your purposes,

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

So your complete code should now look like this;

<?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();

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

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

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.

Source Link
Irata
  • 4.4k
  • 3
  • 9
  • 21

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;

You can also remove this block from the sample you picked up if you have no need for session data in a simple function.

/*
 * 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');

And I am not sure but I don't think this line is required either for your purposes,

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

So your complete code should now look like this;

<?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();

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

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