joomla / joomla-cms

Home of the Joomla! Content Management System
https://www.joomla.org
GNU General Public License v2.0
4.73k stars 3.64k forks source link

[4.x][com_ajax] Ajax call error from the module #37012

Open arenamax opened 2 years ago

arenamax commented 2 years ago

Ajax is not available by namespace!

Steps to reproduce the issue

Namespace from install xml
   <namespace path="src">Am\Module\Amap</namespace>
   ...
js
Joomla.request({
    url: "index.php?option=com_ajax&module=amap&method=getReloadLayoutParams&format=json",
    method: "POST",
    headers: {
        "Cache-Control" : "no-cache",
        "Content-Type" : "application/json"
    },
    onBefore: function (xhr){
        console.log("onBefore");
    },

    onSuccess: function (response, xhr){
        if (response !== ""){
            let res = JSON.parse(response);
            console.log(res.message);
        }
        console.log("onSuccess");
    },

        onError: function(xhr){
        console.log("onError");
    },

    onComplete: function (xhr){
        console.log("onComplete");
    }
});
AmapHelper.php

path 'modules/mod_amap/src/Helper/AmapHelper.php'

<?php
namespace Am\Module\AMAP\Site\Helper;

\defined( '_JEXEC' ) or die;

class AmapHelper
{
        public static function getReloadLayoutParamsAjax()
    {
        $html = array();
        $html['success'] = true;
        $html['message'] = 'Ok';
        $html['messages'] = '';
        $html['data'] = array();

        echo json_encode($html);
                exit;
    }
}

Expected result

JSON data

Actual result

Console log
onBefore
File mod_amap/helper.php does not exist. // ??? It doesn't exist anyway
onSuccess
onComplete

System information (as much as possible)

Joomla v. 4.1.0-rc4 PHP 7.4.27

wedal commented 2 years ago

I confirm the problem.

In /components/com_ajax/ajax.php

$moduleInstance = $app->bootModule('mod_' . $module, $app->getName());

no helper class returned for a helper file in a subdirectory /src/Helper.

As a solution can use: https://github.com/dgrammatiko/invalidate-cache/blob/d19f7f1b6b375dfcc4417d84c8ac3697097a84fd/src/invalidatecache/services/provider.php

But that's not good, I think.

dgrammatiko commented 2 years ago

But that's not good, I think.

This is the Joomla 4 way which is not 100% B/C because of namespacing...

wedal commented 2 years ago

Creating a separate file in a module just to be able to use com_ajax is definitely a problem.

I don't understand why when com_ajax knows the exact location and file name of a module class, we have to load that class manually in a separate file.

Also, there is no documentation about this in the com_ajax usage description.

Ruud68 commented 2 years ago

Running into the same issue.

In J4 Module Helper are namespaced and in src directory. com_ajax is apparently not able to load this helper file and then falls back to J3 file locations > 'mod_module'/helper.php which is not correct.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/37012.

Ruud68 commented 2 years ago

You can try this yourself on a J4 site: [yourdomain/index.php?option=com_ajax&module=articles_news&method=test&format=raw (note: a Joomla core module!)

which results in: RuntimeException: The file at mod_articles_news/helper.php does not exist.


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/37012.

richard67 commented 2 years ago

Closing as having a pull request. Please test #37398 . Thanks in advance.