# CMS Events

The Events system in CMS Full Form allows you to execute custom actions when specific system events occur. This enables you to extend and customize the functionality of the CMS without modifying the core source code directly.

***

## Events Directory Structure

```plaintext
application/
└── Events/
    ├── Backend/
    │   ├── ExampleEvent.php
    │   └── ...
    └── Frontend/
        ├── ExampleEvent.php
        └── ...
```

* `Backend/`: Contains events related to the admin panel or backend operations.
* `Frontend/`: Contains events related to the public-facing side of the website.

***

## Creating a New Event

Inside the appropriate folder (`Backend` or `Frontend`), create a new PHP file for your event, e.g., `ExampleEvent.php`.

```php
<?php

namespace App\Events\Frontend;

use System\Libraries\Event;

class ExampleEvent extends Event
{
    public function __construct($data)
    {
        $this->data= $data;
    }

    public function handle()
    {
        // Perform an action when the event is triggered
        echo "CMS Full Form Event: Data: " . json_encode($this->data) . "<br>";
    }
}
```

* `$this->data`: Stores the data passed in when the event is triggered.
* `handle()`: This method contains the logic that should run when the event is executed.

***

## Using Event

```php
<?php
namespace App\Controllers\Frontend;

use System\Core\BaseController;

class UsersController extends BaseController
{
    public function index()
    {
        // Process data
        ........
        
        //------------------------------ Call 1 event ------------------------------
        // Setup data
        $data = "Welcome to CMS Full Form Event";
        
        // Call event
        \System\Libraries\Events::run('Frontend\ExampleEvent', $data);
        
        //--------------------------- Call Multiple event --------------------------
        $events = [
            'Frontend\ExampleEvent' => "Example Event",
            'Frontend\AnotherEvent' => "Another Event",
        ];
        
        \System\Libraries\Events::runs($events);
    }
}
```

***

{% hint style="warning" %}
**Note**

* **Use clear event naming:** Follow consistent naming conventions to make event management easier and more organized.
* **Handle exceptions within events:** Ensure that exceptions are properly caught and handled inside the `handle()` method to avoid disrupting the main application flow.
* **Optimize performance:** If the event performs heavy tasks, consider using a **queue system** to handle the process asynchronously.
  {% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cmsfullform.com/cms/events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
