Help With Custom PHP Page Joomla 4 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
sarahmx
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Sat Feb 04, 2023 9:05 am

Help With Custom PHP Page Joomla 4

Post by sarahmx » Thu Sep 21, 2023 7:29 am

Hi

i have this custom php page example for Joomla 4.. that i can access from the root of my site mysite.com/custom.php

Code: Select all

<?php
define('_JEXEC', 1);    
use Joomla\CMS\Factory;
//Root Page - // save and run from the main joomla folder
define('JPATH_BASE', realpath(dirname(__FILE__) . '../')); 
//Sub Folder - // save and run from a subfolder under the main joomla folder
//define('JPATH_BASE', realpath(dirname(__FILE__) . '/..')); 

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

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

$crlf		= PHP_EOL;
$app	= new Joomla\CMS\Application\SiteApplication();
\Joomla\CMS\Factory::$application = $app;

// database connection
$db     = Factory::getDbo();
$query = $db->getQuery(true);
$query
   ->select('COUNT(*)')
   ->from($db->quoteName('#__users'));
$db->setQuery($query);
$count   = $db->loadResult();
echo 'PHP version ' . phpversion() . $crlf;
echo $count . ' users' . $crlf;

?>
i would like to try get the user information and i put this code below before the closing php tag at the end of the above php page code but i received 500 error..

Code: Select all

$user = JFactory::getUser();
echo $user->id;
how do i code this properly in Joomla 4

thank you

sarahmx
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Sat Feb 04, 2023 9:05 am

Re: Help With Custom PHP Page Joomla 4

Post by sarahmx » Fri Sep 22, 2023 8:05 am

Hi

i have figured this out myself,the error was session interface not set

based on this topic
viewtopic.php?f=831&t=1003589&sid=8573e ... 7d12182630

i changed my code and now it worked..

Code: Select all

<?php
define('_JEXEC', 1);   
use Joomla\CMS\Factory;
use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;

//Root Page - // save and run from the main joomla folder
define('JPATH_BASE', realpath(dirname(__FILE__) . '../')); 
//Sub Folder - // save and run from a subfolder under the main joomla folder
//define('JPATH_BASE', realpath(dirname(__FILE__) . '/..')); 

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

$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;

    $app->createExtensionNamespaceMap();
 $userSession = \Joomla\CMS\Factory::getApplication()->getSession();

$user=Factory::getUser();
echo $user->id;
echo "<br>";
echo $user->name;

?>


Post Reply

Return to “Joomla! 4.x Coding”