Skip to content

Component Parameters Service

Daniele Rosario edited this page Jul 20, 2015 · 3 revisions

Components Parameters Service

Every Joomla! Component has the possibility to easily add configuration parameters through a config.xml file placed in the root of the components administrator folder (/administrator/components/com_foobar).

This automatically provides the developer with an "options" button in his component's backend that renders various types of configuration fields. This service makes it easier to access and store these parameters from anywhere in your code.

Read the parameters

To read the parameters values from within your component, just call the service through your container:

$yourParam = $this->container->params->get('paramName', 'defaultValue');

The parameters are loaded from the database when the service is initialize for the first time. If later on in your component's workflow these parameters change, you can reload them from the database using the method reload():

$this->container->params->reload();

Store and Save the parameters

You can also change the parameters value from anywhere in your code. To set the new values, just call

$this->container->params->set('paramName', 'newValue');

You can also set multiple values at the same time.

$this->container->params->set(array('newParam1' => 'newValue1', 'newParam2' => 'newValue2'));

After you've set your parameters new values, you can persist them to the database by calling

$this->container->params->save();

Note: if you don't do this, the new values will NOT be actually saved.

Clone this wiki locally