0

I'm writing several joomla modules and I'd like to put common functions in the libraries folder. However I want to use them from a joomla Ajax call in my module. I'm preparing to upgrade from 3.x to 4.x

I have the Ajax working fine. The part I can't get to work is using the USE php.

I have:

libraries/practicemanagement/patientForm.php

and in my module i have in the entry php file

defined('_JEXEC') or die;

$doc = JFactory::getDocument();

require_once 'libraries/practicemanagement/patientForm.php';
require_once __DIR__ . '/helper.php';


$siteButtons  = modPatientChargeCheckingHelper::getSiteButtons();
require JModuleHelper::getLayoutPath('mod_patient_charge_checking', $params->get('layout', 'default'));

the patietForm.php contains two functions:

<?php
trait patientForm
{
    /**
     * The Main Active Language
     *
     * @var      string
     *
     * @since  3.0.9
     */
    public static function getPxAddress($addressId){...}

    public static function getSiteButtons($siteToUse =""){...}
}

The helper class has:

defined('_JEXEC') or die;

class  modPatientChargeCheckingHelper

{

    use patientForm;

    public static function getAjax(){}

}

When the file loads it runs the getSiteButtons() function however I think this is running just because it is included via the require_once statement. When I try to use the Helper Ajax function it just gives a 500 error. I've searched the server log and there doesn't seem to be an issue. I've never used USE before so I'm guessing that I'm doing something wrong. I just need a pointer to help me figure out what.

thanks

php 7.4.33 Joomla! 3.10.11 Stable

2 Answers 2

2

The entry file is not used when calling AJAX. You have to move the loading of patientForm.php into the helper file, before the class declaration:

defined('_JEXEC') or die;

require_once 'libraries/practicemanagement/patientForm.php';

class modPatientChargeCheckingHelper
{
    use patientForm;

    public static function getAjax(){}

}
1
  • Worked!! I'm very happy thanks :) Mar 28, 2023 at 8:52
0

More or less a similar approach as what @Sharky has provided however my example is from a working Component, not a Module, so I am not sure if that is going to be an issue for you. It replaces the require_once with a Use statement for the library.

namespace Irata\Component\Mycomponent\Administrator\View\Barney;

\defined('_JEXEC') or die;

use ...
use Irata\Component\MyComponent\Administrator\Library\FredsTrait;

class HtmlView extends BaseHtmlView
{
    use FredsTrait;
....
}

I create a Component specific Library in my extensions therefore if your Modules are part of a package relating to a single component you might find this is an alternative to using the site wide library aproach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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