# Read module configuration

A detailled explanation of our config service can be found [here](https://docs.hostware.io/developer-documentation/core/config-service).

## Retrieve config value

To use the module configuration, you can simply use the following function.

The first parameter defines the config key, the second the Saleschannel ID (false if the channel should be automatically detected, default) and the third defines the default value if this config value was not yet configured.

```php
$configValue = app(ConfigService::class)->get('core.domain.status', false, null);
```

To retreive a config value in a twig template, you can use this function with the identical parameters:

```twig
{% set configValue = config('core.domain.status', false, null) %}
```

## Listen to the config save

Read our documentation for [listening to events](https://docs.hostware.io/developer-documentation/module/advanced/listen-to-events) first. This is just a quick example.

In your module directory at /Providers/, create an event service provider, e.g.:

{% code title="HwMollieEventServiceProvider.php" %}

```php
<?php

namespace custom\modules\HwMollie\Providers;

use App\Events\ConfigElementsChangedEvent;
use custom\modules\HwMollie\Listeners\ConfigChangedListener;
use Illuminate\Foundation\Support\Providers\EventServiceProvider;

class HwMollieEventServiceProvider extends EventServiceProvider
{
    protected $listen = [
        ConfigElementsChangedEvent::class => [
            ConfigChangedListener::class // This is the event listener you have to create in the next step!
        ]
    ];
}

```

{% endcode %}

Now add your event service provider to the modules.json as a child to the parameter "providers", e.g.:

```json
{
    ...
    "providers": [
        "custom\\modules\\HwMollie\\Providers\\HwMollieServiceProvider", // This is default
        "custom\\modules\\HwMollie\\Providers\\HwMollieEventServiceProvider" // This is the event service listener you just created
    ],
    ...
}
```

In your event listener, you can handle the ConfigElementsChanged event and check if any configuration of your module was changed.

{% hint style="info" %}
Please note that every configuration change will trigger this event, even from other modules. Therefore you need to check if any config item from your module was changed.
{% endhint %}

```php
<?php

namespace custom\modules\HwMollie\Listeners;

use App\Events\ConfigElementsChangedEvent;
use custom\modules\HwMollie\Service\PaymentMethodService;
use Illuminate\Support\Str;

class ConfigChangedListener {
    public function handle(ConfigElementsChangedEvent $event) {
        $mollieChanged = false;

        foreach ($event->payload as $payloadItem) {
            if (Str::contains($payloadItem['key'], "HwMollie.")) { // Enter your module name here
                $mollieChanged = true;
            }
        }

        if ($mollieChanged) {
            // if any mollie key got changed, execute a custom action!

            //$paymentMethodService = app(PaymentMethodService::class);
            //$paymentMethodService->installOrUpdateHandlers();
        }
    }
}

```
