Advertisement

i have an issue how to use com-ajax !

For Joomla! 5.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
sofian10
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 126
Joined: Sat Dec 24, 2016 9:26 am

i have an issue how to use com-ajax !

Post by sofian10 » Fri Jan 31, 2025 7:03 pm

hi
i am trying to add auto load contents to my existing module by using joomla ajax-com .

i created a helper file in the root of the module like this :
mod_mymodulename/helper.php

i added in the helper.php a function like this :
static function getDataAjax() { my code}

i added it in the xml file like this :
<folder>helper.php</folder> under <files>

i added in tmpl/default.php js code like this :
(this is apart of the code )

Code: Select all

 url: 'index.php?option=com_ajax&module=mod_mymodulename&method=getData&format=json',
on the site i get this error message :

Code: Select all

the method getDataAjax not found 
where is my fault ?
why the code cant find the getDataAjax ?

Advertisement
sofian10
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 126
Joined: Sat Dec 24, 2016 9:26 am

Re: i have an issue how to use com-ajax !

Post by sofian10 » Sat Feb 01, 2025 7:27 am

sofian10 wrote: Fri Jan 31, 2025 7:03 pm i added it in the xml file like this :
<filename>helper.php</filename> under <files>

i cant edit the main post

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 31663
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: i have an issue how to use com-ajax !

Post by Per Yngve Berg » Sat Feb 01, 2025 9:51 am

Mod. Note: Relocated topic from Beginner to the Coding Forum.

sofian10
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 126
Joined: Sat Dec 24, 2016 9:26 am

Re: i have an issue how to use com-ajax !

Post by sofian10 » Sat Feb 01, 2025 12:28 pm

i notice that this code is from joomla 3 :

Code: Select all

function load_videos() {
    let nusers = document.querySelector('.load-more-videos');
    Joomla.request({
        url: 'index.php?option=com_ajax&module=mymodulename&method=getData&format=json',
        method: 'GET',
        onSuccess(data) {
            const response = JSON.parse(data);
            if (response.success) {
                nusers.innerHTML = response.data;         
            } else {
                const messages = {"error": [response.message]};
                Joomla.renderMessages(messages);
            }
        },
        onError(xhr) {
            Joomla.renderMessages(Joomla.ajaxErrorsMessages(xhr));
            const response = JSON.parse(xhr.response);
            Joomla.renderMessages({"error": [response.message]}, undefined, true);
        }
    });
}
how to upgrade it to works on joomla 5 ?

the code block on this post cant work !!!

neo314
Joomla! Intern
Joomla! Intern
Posts: 62
Joined: Sat Mar 13, 2010 4:22 am

Re: i have an issue how to use com-ajax !

Post by neo314 » Sun Feb 02, 2025 2:28 am

You need to be sure your helper php file is registered in your provider file, not just creating it. Otherwise the helper won't be found. This is from the module tutorial:

Code: Select all

<?php

\defined('_JEXEC') or die;

use Joomla\CMS\Extension\Service\Provider\Module as ModuleServiceProvider;
use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory as ModuleDispatcherFactoryServiceProvider;
use Joomla\CMS\Extension\Service\Provider\HelperFactory as HelperFactoryServiceProvider;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;

return new class () implements ServiceProviderInterface {

    public function register(Container $container): void
    {
        $container->registerServiceProvider(new ModuleDispatcherFactoryServiceProvider('\\My\\Module\\Hello'));
        $container->registerServiceProvider(new HelperFactoryServiceProvider('\\My\\Module\\Hello\\Site\\Helper'));
        $container->registerServiceProvider(new ModuleServiceProvider());
    }
};
See the line: $container->registerServiceProvider(new HelperFactoryServiceProvider('\\My\\Module\\Hello\\Site\\Helper'));

Look at step 8 and 9 of the module tutorial

Joomla has to be able to load your module, find it's registered helper file and then find the right function.

sofian10
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 126
Joined: Sat Dec 24, 2016 9:26 am

Re: i have an issue how to use com-ajax !

Post by sofian10 » Thu Feb 06, 2025 7:01 pm

the main issue in my module is :
the developer said :
Our current module is built on the Joomla 3 structure but functions well with Joomla 5 in standard mode .At the moment, we do not support com_ajax .
now i converted it to joomla 5 structure by following the module tutorial .
so this is what i did :
1- i added these new files to convert the module to j5 structure :
public_html/modules/mod_mymodule/script.php
public_html/modules/mod_mymodule/services/provider.php
public_html/modules/mod_mymodule/src/Dispatcher/Dispatcher.php

public_html/media/mod_mymodule/joomla.asset.json
public_html/media/mod_mymodule/js/add-suffix.js

i added this function in the original helper file (original = from the developer) :

Code: Select all

public function loadvideosAjax()

added in this file :
public_html/modules/mod_mymodule/src/Helper/MymoduleHelper.php
2- now i have an error say:
the file helper.php not found .
i dont know why it call helper.php , i added the function in MymoduleHelper.php

how can i address the js code to MymoduleHelper.php ?

this is the provider.php code :

Code: Select all

<?php

\defined('_JEXEC') or die;

use Joomla\CMS\Extension\Service\Provider\Module as ModuleServiceProvider;
use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory as ModuleDispatcherFactoryServiceProvider;
use Joomla\CMS\Extension\Service\Provider\HelperFactory as HelperFactoryServiceProvider;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;

return new class () implements ServiceProviderInterface {

public function register(Container $container): void
{
$container->registerServiceProvider(new ModuleDispatcherFactoryServiceProvider('\\PluginsWare\\Module\\Mymodule'));
$container->registerServiceProvider(new HelperFactoryServiceProvider('\\PluginsWare\\Module\\Mymodule\\Site\\Helper'));
$container->registerServiceProvider(new ModuleServiceProvider());
}
};

this is the dispatcher.php code :

Code: Select all

<?php

namespace PluginsWare\Module\Mymodule\Site\Dispatcher;



\defined('_JEXEC') or die;

use Joomla\CMS\Dispatcher\AbstractModuleDispatcher;
use Joomla\CMS\Helper\HelperFactoryAwareInterface;
use Joomla\CMS\Helper\HelperFactoryAwareTrait;
use Joomla\CMS\Language\Text;
use PluginsWare\Module\Mymodule\Site\MymoduleHelper;

class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
{
use HelperFactoryAwareTrait;

}

this is the js code :

Code: Select all

function loadmore_videos() {
let loadvideos = document.querySelector('.sof-videos-row');
Joomla.request({
url: 'index.php?option=com_ajax&module=yendifvideoshare_videos&method=loadvideos&format=json',
method: 'POST',
onSuccess(data) {
const response = JSON.parse(data);
if (response.success) {
loadvideos.innerHTML = response.data;
} else {
const messages = {"error": [response.message]};
Joomla.renderMessages(messages);
}
},
onError(xhr) {
Joomla.renderMessages(Joomla.ajaxErrorsMessages(xhr));
const response = JSON.parse(xhr.response);
Joomla.renderMessages({"error": [response.message]}, undefined, true);
}
});
}

when i click on this button :

Code: Select all

 <div onClick="loadmore_videos()" class="sof-loadmore-button" >see more videos</div>
i get error :
the file helper.php not found .

any help ?

neo314
Joomla! Intern
Joomla! Intern
Posts: 62
Joined: Sat Mar 13, 2010 4:22 am

Re: i have an issue how to use com-ajax !

Post by neo314 » Thu Feb 06, 2025 7:51 pm

There are a number of places where this could go wrong, but I think the line in the dispatcher file should be:

use PluginsWare\Module\Mymodule\Site\Helper\MymoduleHelper;

sofian10
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 126
Joined: Sat Dec 24, 2016 9:26 am

Re: i have an issue how to use com-ajax !

Post by sofian10 » Fri Feb 07, 2025 12:14 am

neo314 wrote: Thu Feb 06, 2025 7:51 pm There are a number of places where this could go wrong, but I think the line in the dispatcher file should be:

use PluginsWare\Module\Mymodule\Site\Helper\MymoduleHelper;
i fixed the line you mention , but the issue not solved .
this is the error i get :

The file at mod_Mymodule/helper.php does not exist.

sofian10
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 126
Joined: Sat Dec 24, 2016 9:26 am

Re: i have an issue how to use com-ajax !

Post by sofian10 » Fri Feb 07, 2025 12:27 am

my issue is com-ajax it self .
com-ajax built to work like this :

Code: Select all

 $helperFile = JPATH_BASE . '/modules/mod_' . $module . '/helper.php';
so it cant support my module helper .

i will try to create the helper.php file .

neo314
Joomla! Intern
Joomla! Intern
Posts: 62
Joined: Sat Mar 13, 2010 4:22 am

Re: i have an issue how to use com-ajax !

Post by neo314 » Fri Feb 07, 2025 12:34 am

Maybe the code from the stats dispatcher will help you...

Code: Select all

class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
{
    use HelperFactoryAwareTrait;

    /**
     * Returns the layout data.
     *
     * @return  array
     *
     * @since   5.1.0
     */
    protected function getLayoutData()
    {
        $data = parent::getLayoutData();

        $data['list'] = $this->getHelperFactory()->getHelper('StatsHelper')->getStats($data['params'], $this->getApplication());

        return $data;
    }
}

sofian10
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 126
Joined: Sat Dec 24, 2016 9:26 am

Re: i have an issue how to use com-ajax !

Post by sofian10 » Fri Feb 07, 2025 2:06 am

neo314 wrote: Fri Feb 07, 2025 12:34 am Maybe the code from the stats dispatcher will help you...

Code: Select all

class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface
{
    use HelperFactoryAwareTrait;

    /**
     * Returns the layout data.
     *
     * @return  array
     *
     * @since   5.1.0
     */
    protected function getLayoutData()
    {
        $data = parent::getLayoutData();

        $data['list'] = $this->getHelperFactory()->getHelper('StatsHelper')->getStats($data['params'], $this->getApplication());

        return $data;
    }
}
i get this error :
Call to a member function getStats() on null

neo314
Joomla! Intern
Joomla! Intern
Posts: 62
Joined: Sat Mar 13, 2010 4:22 am

Re: i have an issue how to use com-ajax !

Post by neo314 » Fri Feb 07, 2025 5:15 am

That means it's not getting your helper.

sofian10
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 126
Joined: Sat Dec 24, 2016 9:26 am

Re: i have an issue how to use com-ajax !

Post by sofian10 » Fri Feb 07, 2025 5:50 am

neo314 wrote: Fri Feb 07, 2025 5:15 am That means it's not getting your helper.
i have the very important question :
the component com-ajax has these 2 codes :

Code: Select all

 $helperFile = JPATH_BASE . '/modules/mod_' . $module . '/helper.php';

Code: Select all

 elseif ($results === null) {
            // The helper file does not exist
            $results = new RuntimeException(Text::sprintf('COM_AJAX_FILE_NOT_EXISTS', 'mod_' . $module . '/helper.php'), 404);
        }
this mean it will find helper.php file .
but in the module tutorial "mod_hello" , the module hello has :
/mod_hello/src/Helper/HelloHelper.php

how com-ajax can find HelloHelper.php in module hello ??
why it not show error : The file at mod_Hello/helper.php does not exist ?

where is the secret ?

naft2e
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Wed Feb 19, 2025 3:05 pm

Re: i have an issue how to use com-ajax !

Post by naft2e » Wed Feb 19, 2025 3:16 pm

Since you’re using the HelperFactory, you don’t actually need to manually import inside your dispatcher with

Code: Select all

use PluginsWare\Module\Mymodule\Site\MymoduleHelper;
So you can delete that.

Make sure your helper file is also properly namespaced. You should include this in your src/Helper/Helper.php

Code: Select all

PluginsWare\Module\MyModule\Site\Helper\MymoduleHelper

Advertisement

Post Reply

Return to “Joomla! 5.x Coding”