# Listen to events

### Listen to Events

To listen to events of the core or of another module, you first must create an EventServiceProvider which needs to be registered in your module.json file.

Following you can see a basic event listener which reacts to the change of config elements. This file is located in your module folder at Listeners/ConfigChangedListener.php.

```php
<?php

namespace custom\modules\HwMollie\Listeners;

use App\Events\ConfigElementsChangedEvent;

class ConfigChangedListener {

    /**
     * Handle the event.
     *
     * @param ConfigElementsChangedEvent $event
     * @return void
     */
    public function handle(ConfigElementsChangedEvent $event) {
        dump("config changed event");
        dd($event->payload);
    }
}
```

However, this listener must be configured before it will be in action. Therefore, you must create one event service provider class for your module, where you will map events to listener.\
In this case, the file is called HwMollieEventServiceProvider.php located in the /Providers folder of your module:

```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
        ]
    ];
}
```

As you can see, the class only contains a $listen array where the mapping takes place. An event can have multiple listeners. In this case, the `ConfigElementsChangedEvent` Event got mapped to the listener we created previously `ConfigChangedListener.`

Finally, you must register the event service provider in your module.json file. To do that, you can copy the line for the base service provider:

```json
...
"providers": [
    "custom\\modules\\HwMollie\\Providers\\HwMollieServiceProvider",
    "custom\\modules\\HwMollie\\Providers\\HwMollieEventServiceProvider" // our event service provider
],
...
```

Thats it! You now have created an unique event listener to listen to events in h0stware.
