I'm quite new to Joomla. I have a component with multiple different models and views, but a few of them use the same functions. Right now I have those functions copied in every model file, but I'd prefer to just have a single file with those functions.
After looking up solutions on the internet, the right way to do it seems to be to create a helper class somewhere in the component, but there's so much contradictory information that I'm not sure where I'm supposed to put it, and how to access it from my model files.
My component has a "site" directory, and in it, there are "models" and "views". I added a third folder, "helpers". Inside it, I made a MyHelper.php file like this:
defined('_JEXEC') or die; class MyHelper { public static function meep() { echo "Meep! Helper works!"; } } But how do I access it from inside my model php files? I've tried various pieces of code I found online, but nothing seems to work.
First, I tried putting this line in my model file:
use Joomla\Component\[my component name]\Site\Helper\MyHelper; and then later inside a function:
MyHelper::meep(); However, when I open the page, I get this error right in the page title:
Class "Joomla\Component\[my component name]\Site\Helper\MyHelper" not found So I tried replacing that "use" line with this, which I found somewhere else on the internet:
JLoader::register('MyHelper', JPATH_SITE . '/components/com_[my component name]/helpers/MyHelper.php'); And now the error is simply:
Class "MyHelper" not found What am I doing wrong?