Skip to content

Events

Introduction

Event Listeners in UnoPim are a way to implement the observer pattern, where listeners respond to events that occur in the application. Events can be thought of as announcements made by the application, and listeners are the actions taken in response to those announcements. All event classes in UnoPim are stored in the Providers folder, and the listeners are stored in the Listeners folder.

In UnoPim, events and listeners are organized in a clear and structured manner:

  • Events are typically stored in the Events folder.
  • Listeners are stored in the Listeners folder.

This organization makes it easy to manage and locate the event-driven components of your application.

To learn in detail about Controllers, you can visit the Laravel documentation here.

Creating an Event Class

Manually Registering Events

In UnoPim, you register events manually in the boot method of your EventServiceProvider.php file. Here is an example of how to register events:

php
/**
 * Register any other events for your application.
 *
 * @return void
 */
public function boot()
{
    //...

    Event::listen('event.name', 'path-upto-listener@function');
}

In this example, event.name is the name of the event, and path-upto-listener@function is the listener method that will handle the event.

Manually Registering Listeners

When registering events, you specify the listener function to be executed when an event is triggered. Here is an example of how to register a listener:

php
class EventServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        //...

        Event::listen('catalog.attribute.create.after', 'Webkul\Catalog\Listeners\Attribute@handleAttributeCreated');
    }
}

Specifying Events

In UnoPim, events are typically fired before and after the execution of CRUD operations. This allows listeners to perform additional actions, such as logging, notifications, or data manipulation, at specific points in the lifecycle of an operation.

For example, you might have events fired during product creation, updating, or deletion. Here’s an example of firing events before and after saving a product:

php
namespace Webkul\Catalog\Repositories;

use Webkul\Catalog\Contracts\Product;

class ProductRepository extends Repository
{
    public function create(array $data)
    {
        Event::dispatch('catalog.product.create.before', $data);

        $product = parent::create($data);

        Event::dispatch('catalog.product.create.after', $product);

        return $product;
    }
}

Events Fired in UnoPim

In UnoPim, there are several events fired throughout its operations, allowing developers to hook into specific points in the application's lifecycle to customize behavior or add functionality. Here's a list of events that are fired in UnoPim, which you can listen to and handle as needed by creating event listeners:

Events nameFunctionality
admin.password.update.afterThis event will be fired after admin password gets updated.
catalog.attribute.create.beforeThis event is fired before an attribute is created.
catalog.attribute.create.afterThis event is fired after an attribute is created.
catalog.attribute.delete.beforeThis event is fired before an attribute is deleted.
catalog.attribute.delete.afterThis event is fired after an attribute is deleted.
catalog.attribute.update.beforeThis event is fired before an attribute is updated.
catalog.attribute.update.afterThis event is fired after an attribute is updated.
catalog.attribute_family.create.beforeThis event is fired before an attribute family is created.
catalog.attribute_family.create.afterThis event is fired after an attribute family is created.
catalog.attribute_family.delete.beforeThis event is fired before an attribute family is deleted.
catalog.attribute_family.delete.afterThis event is fired after an attribute family is deleted.
catalog.attribute_family.update.beforeThis event is fired before an attribute family is updated.
catalog.attribute_family.update.afterThis event is fired after an attribute family is updated.
catalog.attribute.group.create.beforeThis event is fired before an attribute group is created.
catalog.attribute.group.create.afterThis event is fired after an attribute group is created.
catalog.attribute.group.delete.beforeThis event is fired before an attribute group is deleted.
catalog.attribute.group.delete.afterThis event is fired after an attribute group is deleted.
catalog.attribute.group.update.beforeThis event is fired before an attribute group is updated.
catalog.attribute.group.update.afterThis event is fired after an attribute group is updated.
catalog.categories.mass-update.beforeThis event is fired before a bulk category update.
catalog.categories.mass-update.afterThis event is fired after a bulk category update.
catalog.category.create.beforeThis event is fired before a category is created.
catalog.category.create.afterThis event is fired after a category is created.
catalog.category.delete.beforeThis event is fired before a category is deleted.
catalog.category.delete.afterThis event is fired after a category is deleted.
catalog.category.update.beforeThis event is fired before a category is updated.
catalog.category.update.afterThis event is fired after a category is updated.
catalog.category_field.create.beforeThis event is fired before a category_field is created.
catalog.category_field.create.afterThis event is fired after a category_field is created.
catalog.category_field.delete.beforeThis event is fired before a category_field is deleted.
catalog.category_field.delete.afterThis event is fired after a category_field is deleted.
catalog.category_field.update.beforeThis event is fired before a category_field is updated.
catalog.category_field.update.afterThis event is fired after a category_field is updated.
catalog.product.create.beforeThis event is fired before a product is created.
catalog.product.create.afterThis event is fired after a product is created.
catalog.product.delete.beforeThis event is fired before a product is deleted.
catalog.product.delete.afterThis event is fired after a product is deleted.
catalog.product.update.beforeThis event is fired before a product is updated.
catalog.product.update.afterThis event is fired after a product is updated.
core.channel.create.beforeThis event will be fired before a channel gets created.
core.channel.create.afterThis event will be fired after a channel gets created.
core.channel.delete.beforeThis event will be fired before a channel gets deleted.
core.channel.delete.afterThis event will be fired after a channel gets deleted.
core.channel.update.beforeThis event will be fired before a channel gets updated.
core.channel.update.afterThis event will be fired after a channel gets updated.
core.configuration.save.beforeThis event will be fired before configuration save.
core.configuration.save.afterThis event will be fired after configuration save.
core.currency.create.beforeThis event will be fired before currency gets created.
core.currency.create.afterThis event will be fired after currency gets created.
core.currency.delete.beforeThis event will be fired before currency gets deleted.
core.currency.delete.afterThis event will be fired after currency gets deleted.
core.currency.update.beforeThis event will be fired before currency gets updated.
core.currency.update.afterThis event will be fired after currency gets updated.
core.locale.create.beforeThis event will be fired before a locale gets created.
core.locale.create.afterThis event will be fired after a locale gets created.
core.locale.delete.beforeThis event will be fired before a locale gets deleted.
core.locale.delete.afterThis event will be fired after a locale gets deleted.
core.locale.update.beforeThis event will be fired before a locale gets updated.
core.locale.update.afterThis event will be fired after a locale gets updated.
data_transfer.exports.batch.export.afterThis event will be fired after batch exports data_transfer.
data_transfer.exports.batch.export.beforeThis event will be fired before batch exports data_transfer.
data_transfer.exports.completedThis event will be fired if exports completed.
data_transfer.exports.create.afterThis event will be fired after exports data_transfer is created.
data_transfer.exports.create.beforeThis event will be fired before exports data_transfer is created.
data_transfer.exports.export.now.beforeThis event will be fired before export data_transfer is updated.
data_transfer.exports.startedThis event will be fired if exports started.
data_transfer.exports.update.afterThis event will be fired after export data_transfer is updated.
data_transfer.exports.update.beforeThis event will be fired before export data_transfer is updated.
data_transfer.imports.batch.export.afterThis event will be fired after batch imports data_transfer.
data_transfer.imports.batch.export.beforeThis event will be fired before batch imports data_transfer.
data_transfer.imports.completedThis event will be fired if imports completed.
data_transfer.imports.create.afterThis event will be fired after imports data_transfer is created.
data_transfer.imports.create.beforeThis event will be fired before imports data_transfer is created.
data_transfer.imports.indexingThis event will be fired if imports indexing.
data_transfer.imports.linkingThis event will be fired if imports linking.
data_transfer.imports.startedThis event will be fired if imports started.
data_transfer.imports.update.afterThis event will be fired after imports data_transfer is updated.
data_transfer.imports.update.beforeThis event will be fired before imports data_transfer is updated.
data_transfer.imports.validate.afterThis event will be fired after imports validate.
data_transfer.imports.validate.beforeThis event will be fired before imports validate.
products.datagrid.syncThis event is fired to sync the product datagrid.
user.admin.create.afterThis event will be fired after admin gets created.
user.admin.create.beforeThis event will be fired before admin gets created.
user.admin.delete.afterThis event will be fired after admin gets deleted.
user.admin.delete.beforeThis event will be fired before admin gets deleted.
user.admin.update.afterThis event will be fired after admin gets updated.
user.admin.update.beforeThis event will be fired before admin gets updated.
user.api_integration.create.afterThis event will be fired after API integration is created.
user.api_integration.create.beforeThis event will be fired before API integration is created.
user.api_integration.update.afterThis event will be fired after API integration is updated.
user.api_integration.update.beforeThis event will be fired before API integration is updated.
user.api_key.delete.afterThis event will be fired after API key is deleted.
user.api_key.delete.beforeThis event will be fired before API key is deleted.
user.role.create.afterThis event will be fired after role gets created.
user.role.create.beforeThis event will be fired before role gets created.
user.role.delete.afterThis event will be fired after role gets deleted.
user.role.delete.beforeThis event will be fired before role gets deleted.
user.role.update.afterThis event will be fired after role gets updated.
user.role.update.beforeThis event will be fired before role gets updated.

Listening to Existing Events

UnoPim uses events and listeners to implement the observer pattern, allowing you to respond to various actions and events within the application. You can listen to specific events and execute custom code when those events are triggered.

Registering a Listener

Open the EventServiceProvider.php file located in the Providers directory of your UnoPim application. This file is where you register event listeners.

Inside the boot() method of EventServiceProvider.php, use the Event::listen method to register your listener. This method takes the event name and a callback function or a class method that will handle the event.

php
Event::listen('catalog.product.create.after', 'Webkul\Notification\Listeners\Product@createNotification');

By registering the listener, you have associated the createNotification function with the catalog.product.create.after event. Whenever this event is triggered, the specified function will be executed.

You can modify the listener function according to your requirements to perform the desired operation.

Released under the MIT License.