2

I am trying to call a php function from the backend of a module (I'm using a custom form field). com_ajax does not work because it pulls the current menu item, which doesn't exist since it is not being called from a menu.

I have tried linking to a php file directly but that will not work because defined('_JEXEC') or die('Direct Access to this location is not allowed.'); is needed for security purposes.

I have also tried using JURI::base() . 'index.php?option=mod_myMod&task=myPhpFunction'; for my AJAX request URL while having the code block below defined before my helper class in the module's helper.php but just get a 404 (Component not found.) error:

$taskInput = JFactory::getApplication()->input;
$task = $taskInput->get('task');

if ($task == "importData") {
    ModPropertySearchHelper::importDataAjax();
}

I could create a separate component to handle this. However, that just feels weird and is a problem com_ajax tried to solve, so I would rather not do that.

Is there a way I could override ajax.php in order to support being called from a module's backend? I wouldn't want to replace it in case of updates and such.

Or is there a better solution to my problem?

  • What do you mean from a "module's backend"? Are you referring to a module in the Joomla backend (admin panel)? – Lodder Mar 22 '16 at 23:07
  • 1
    The part of the admin panel where you can configure various settings of a module. Where the form fields specified in the module's XML file would show up. I am using a custom form field for this module which needs access to a PHP function in order to send user-provided data to the database. – Kibner Mar 22 '16 at 23:22
  • Ahh I see. Not tried this before. Maybe try using index.php?option=com_modules&task=myFunction....note the com_modules as this is the component being used – Lodder Mar 22 '16 at 23:28
  • I just tried doing that and got back a response with a bunch of html that appeared to be the basic module page for my module, but with no form fields or content. – Kibner Mar 22 '16 at 23:39
  • Can you explain what is the exact problem that you are trying to solve? Why are you interested for a menu item id - how is it involved with the ajax results in the backend of the module? – FFrewin Apr 22 '16 at 12:08
1

For security, there are several measures in Joomla that prevent direct access to scripts.

So, you have to follow Joomla good practices to take advantage of Joomla CMS.

To execute inside Joomla, you have to create a component, a module or a plugin.

If you are looking to implement an Ajax query, then you have to create a plugin for com_ajax.

Using Joomla Ajax Interface

A slim, extensible component to act as an entry point for HTTP requests for stand alone modules and plugins, thus allowing for the potential of Ajax functionality in them. Com_ajax is generally used when you are not the developer of the component that the module or plugin is interacting with.

https://docs.joomla.org/Using_Joomla_Ajax_Interface

  • Yes, I would like to use com_ajax, but am unable to due to how it works. It uses the current menu item to append to the url it creates. However, since I'm calling it from the admin configuration side of joomla, that menu item doesn't exist and it gives me an error. That is why I asked if there is a way to override ajax.php (where com_ajax resides). Or if there was some other way to do this without creating a separate component/module/plug-in/etc. to use as an intermediary (which seems to be the main benefit of com_ajax being included in core of joomla). – Kibner Mar 23 '16 at 11:47
  • For a little more detail, here is ajax.php of com_ajax on github: github.com/joomla/joomla-cms/blob/staging/components/com_ajax/… Line 56 is where it fails for me. It is similar to this issue but I can't see a way to add a Itemid to my com_ajax request so that it will use it instead of doing that module check. – Kibner Mar 23 '16 at 12:12
  • It is not an issue. You must follow the API definition. If you call an API that requires an Itemid, just create a menu item to locate a the target following the route definition. Even if you are running from a backend, you can call any Url via Ajax. – Anibal Mar 23 '16 at 17:36
  • com-ajax doesn't accept an Itemid. Can you have a form on the admin configuration be tied to a menu item? – Kibner Mar 23 '16 at 18:09
  • No, backend site is oriented to a single menu of components and modules – Anibal Mar 24 '16 at 9:48
0

It looks like this module would allow you to paste in php, but I have not tried it myself.

But as Anibal says. If you are going to be using Ajax and need to interact with Joomla, you should use com_ajax and develop it yourself.

http://extensions.joomla.org/extension/blank-module

  • Interesting module, but it is for modifying how a module looks/works on the front-end of a joomla site and I need something that will let me execute a php function from an ajax call in the back-end (the admin configuration part of a module). – Kibner Mar 23 '16 at 12:03
  • Sounds like you need to get your hands dirty and code this. – Søren Beck Jensen Mar 23 '16 at 13:06
  • Yeah, I'm creating a separate component right now to do this. :( – Kibner Mar 23 '16 at 13:36
  • Hopefully you are using our component-creator.com to save you some time :-) – Søren Beck Jensen Mar 23 '16 at 14:14

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.