I am running latest Joomla 4.1.4. I am testing a simple PHP script within a subfolder of my Joomla 4 installation: root/assets/scripts/test.php
I would like to make use of the Joomla framework and get the current user_id.
How should I define the JPATH_BASE, if my script is in a subfolder, 2 levels down from the root?
I have already tried to change dirname(__FILE__) and point 2 levels up, but nothing works.
It is a stand alone script on purpose, since I am not yet ready to code a module. It's really hard to find good tutorials on how to code Joomla 4 modules with all there is to a module (like creating database tables, updating tables, backend options, backend CRUD tables ... ). Yes, a Hello World module worked.
Code: Select all
<?php
// reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
// joomla 4
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
// joomla 4 user
$user = JFactory::getUser();
$user_id = $user->get('id');
echo "User:" . $user_id;