JPATH_BASE stand alone php script inside joomla 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.
Locked
DadeMurphy
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Mon Feb 01, 2021 2:39 am

JPATH_BASE stand alone php script inside joomla

Post by DadeMurphy » Sat Jun 25, 2022 6:58 am

Hello,

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;

DadeMurphy
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Mon Feb 01, 2021 2:39 am

Re: JPATH_BASE stand alone php script inside joomla

Post by DadeMurphy » Sat Jun 25, 2022 9:11 am

solved the directory issue, but still not working.

not sure, what else to include for Factory Class.

Code: Select all

// joomla 4
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__DIR__, 2)); // PHP7
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$user = Factory::getUser();
$user_id = $user->get('id');

DadeMurphy
Joomla! Apprentice
Joomla! Apprentice
Posts: 13
Joined: Mon Feb 01, 2021 2:39 am

Re: JPATH_BASE stand alone php script inside joomla

Post by DadeMurphy » Sat Jun 25, 2022 12:47 pm

this will work.

script is located 2 levels below joomla root:

define('JPATH_BASE', dirname(__DIR__, 2));

Solution here: https://groups.google.com/g/joomla-dev- ... 5J2s9hhMxA

Code: Select all

<?php

// reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);

// define joomla
define('_JEXEC', 1);
// define joomla root path based on where the current script is located (2 directories up)
define('JPATH_BASE', dirname(__DIR__, 2));

// custom files
//require_once JPATH_BASE . '/assets/scripts/class.php';

// joomla files
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

// boot joomla directory 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;

// get user info
$user_info = \Joomla\CMS\Factory::getApplication()->getSession()->get('user');

// print
print "<pre>";
print_r($user_info);
print "</pre>";

//exit;

$user_name = $user_info->name;
$user_email = $user_info->email;
$user_username = $user_info->username;
$user_id = $user_info->id;

echo "name:" . $user_name;
echo "<br>";
echo "email:" . $user_email;
echo "<br>";
echo "username:" . $user_username;
echo "<br>";
echo "user id:" . $user_id;
echo "<br>";


Locked

Return to “Joomla! 4.x Coding”