0

I'm using Joomla 3.9.18 and I've got a project including a component and a couple of modules. I've got a couple of variables that are the same throughout the whole project (like some prefixes and a path to an image server), but now when I need to change them, I need to change them everywhere. I want to define these variables in one spot so I only need to change them there.

Where do I define variables like these and how can I access them in my component and modules?

I know configuration.php isn't an option because of core-hacks. I've read something about component options, but I'm still fairly new so I don't really understand how it works. Is this the way to go? And if yes, how does it work (easily explained)?

1

See the documentation for Adding Configuration Parameters.

This then makes your configuration parameters available via the params, these can be obtained anywhere using the JComponentHelper class or via your own method. Example:

JComponentHelper::getParams('com_your_component')->get('yourcustomfield', 'a-fallback-value');

As an alternative, you can create a defines.php at your site root, and include global constants there, as per index.php

1
  • I don't think it's mentioned in the tutorial step, but if in your component's Table class you set the protected variable $_jsonEncode to include your params field (eg $_jsonEncode = array('params');) then Joomla will do the conversion in/out of the json-encoded format which is used to store params, rather than you needing to override the Table bind() function. See libraries/src/Table/Table.php and the example in administrator/components/com_contact/tables/contact.php. – Robbie Jackson Sep 6 '20 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy