2

I have created a component and need to get a constant value in the component administrator and list the item according to it. I have tried different ways to get the constant value in the display() function in 'view.html.php'. I am expecting the value will be available in the default.php with $this.

I have defined the constant in the configuration.php file, a URL is supposed to be define.

define('CONSTANTVALUE', 'the URL string');

in the display() function,

$config = JFactory::getConfig();
$constantvalue = $config->get('CONSTANTVALUE');
$this->constantvalue=  $constantvalue;

Seems this is not working. Then, I have tried:

JFactory::getApplication()->get('CONSTANTVALUE');

That is also not working.

I have referred to this thread, https://stackoverflow.com/questions/21702981/joomla-3-how-to-get-value-from-configuration-file

How can I get the constant from configuration to component view file?

5
  • Welcome to JSE. Please take our tour.
    – mickmackusa
    Oct 19, 2020 at 5:16
  • Please don't add random code to configuration.php.
    – Sharky
    Oct 19, 2020 at 5:17
  • @Adam-KER - Please study how to add component config params to any component in Joomla here: docs.joomla.org/J3.x:Developing_an_MVC_Component/… And then anywhere in your component you can use the ComponentHelper->getParams() method to get your parameter (constant if you like). JoomlaStackexchange is also full of answers on this topic already.
    – Zollie
    Oct 19, 2020 at 6:05
  • getParams() is a static method sorry, correctly again: ComponentHelper::getParams(), however in your own component you will get the component params via your Model as it is given in the document’ link above.
    – Zollie
    Oct 19, 2020 at 6:23
  • 1
    @Zollie Thank you so much for the response and for the link.
    – Adam-KER
    Oct 20, 2020 at 13:28

1 Answer 1

2

So to get it from global configuration set in configuration.php from anywhere within Joomla component, plugins, modules and templates you would use:

$config   = JFactory::getConfig();
$siteName = $config->get('sitename', 'No Site Name Specified');

OR

$siteName  = JFactory::getConfig()->get('sitename', 'No Site Name Specified');

You can also use name spacing in preparation for Joomla 4 with the following:

define

use Joomla\CMS\Factory;

before the class opening.

$globalConfig = Factory::getConfig();
$siteName     = $globalConfig->get('sitename', 'No Site Name Specified'); 

Noting the 'sitename' might be case sensitive.

Your Answer

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

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