1

I want to access the currently logged in user in my Helper function using AJAX.

function editUser(){
    runAjax('editUser',"one");
}
function runAjax(type,regHtml){
    request = {
        'option' : 'com_ajax',
        'module' : 'user_login',
        'cmd'    : type,
        'params' : params,
        'format' : 'json'
    };

    jQuery.ajax({
        type   : 'POST',
        data   : request,
        success: function (response) {

The editUser function is set off via a onmousedown button.

This all works fine and calls the helper function. The issue is on the helper side.

public static function getAjax()
{

    function setUpForm(){

        $user = JFactory::getUser();

        echo "<pre>";
        print_r($user);
        echo "</pre>";

    }


    $input = JFactory::getApplication()->input;
    $params  = $input->get('params',"",raw);

     $cmd  = $input->get('cmd');


    if($cmd == "editUser"){
        setUpForm();
    }

This works but isn't the recommended way of getting the user object. It's:

use Joomla\CMS\Factory;
$user = Factory::getUser();

This is the switch to use cases but this won't work with AJAX because the use won't work out of context I'd need to do something like:

define('JPATH_BASE', realpath(dirname(__FILE__) . '/../..'));
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';

to include extra files (I haven't tried it since it seems a mad way to go)

Is there a way around this.

Whilst I have working code I'm thinking about future proofing my code.

Update

I'm adding some cleaner code to show what doesn't work:

This doesn't work because it doesn't have the namespace context

class modLoginHelper

{

public static function getAjax()
    {
        use Joomla\CMS\Factory as YourNewAlias;
        user = Factory::getUser();
    }
    
}

This won't throw an error but doesn't work

class modLoginHelper

{
    use Joomla\CMS\Factory as YourNewAlias;
    
public static function getAjax()
    {
        
        user = Factory::getUser();
    }
    
}

So whichever way I try to follow the use Joomla ... it won't work - I have to revert to the old way of doing things.

5
  • The question isn't very clear. Are you asking about JFactory::getUser() vs Joomla\CMS\Factory::getUser()?
    – Sharky
    Sep 15, 2020 at 10:44
  • Yes JFactory::getUser() works fine. But use Joomla\CMS\Factory which is the recommended way won't work using an AJAX call Sep 15, 2020 at 12:10
  • ...why won't it work exactly? JFactory is just an aliased namespace for Joomla\CMS\Factory - see the classmap - github.com/joomla/joomla-cms/blob/staging/libraries/… - are you getting the old "Cannot use Joomla\CMS\Factory as Factory because the name is already in use" error? If so, alias that, so something like use \Joomla\CMS\Factory as YourNewAlias and then YourNewAlias::getUser().
    – Grant G
    Sep 15, 2020 at 12:40
  • using use Joomla\CMS\Factory needs to be declared inside the global namespace which doesn't work within an AJAX context I think because the namespace doesn't exist. Sep 15, 2020 at 12:45
  • Not related to your question but you should avoid declaring global functions like setUpForm(), especially since you already have a class.
    – Sharky
    Sep 15, 2020 at 16:13

1 Answer 1

4

When importing classes using use statement, it has to be outside of your class. When put inside a class, it's interpreted as a use of trait. And when used inside a method it will cause a syntax error. In your case it should be used like this:

use Joomla\CMS\Factory;

class ModLoginHelper
{ 
    public static function getAjax()
    {
        $user = Factory::getUser();
        // Your code here...
    }
}

From PHP documentation:

The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped.

https://www.php.net/manual/en/language.namespaces.importing.php

Alternatively, you can use fully qualified class names in your code like this:

class ModLoginHelper
{ 
    public static function getAjax()
    {
        $user = Joomla\CMS\Factory::getUser();
        // Your code here...
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.