Events

Introduction

Maravel’s events provide a simple observer implementation, allowing you to subscribe and listen for events in your application. Event classes are typically stored in the app/Events directory, while their listeners are stored in app/Listeners.

Differences From Maravelith

In general, events in Maravel function exactly like they do in the full-stack Maravelith framework, so please review the full Maravelith documentation. Event broadcasting is even supported in Maravel, which allows you to listen for your server-side events in your client-side JavaScript. However, there are a few minor differences which warrant discussion.

Generators

Unlike legacy micro-frameworks, Maravel includes generator commands to automatically scaffold events and listeners for you, see Artisan. These generated classes provide the basic structure of every event and listener.

Registering Events / Listeners

Like the full Maravelith framework, the EventServiceProvider included with your Maravel application provides a convenient place to register all event listeners. The listen property contains an array of all events (keys) and their listeners (values). Of course, you may add as many events to this array as your application requires:

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'App\Events\ExampleEvent' => [
        'App\Listeners\ExampleListener',
    ],
];

Firing Events

You may use the event helper function or Event facade to fire events throughout your Maravel application. Again, these functions behave exactly like their full Maravelith framework equivalent:

event(new ExampleEvent);

Event::dispatch(new ExampleEvent);

Just like Maravelith, Maravel fully supports Queueable Array Callables for event listeners to bypass PHP serialization overhead and prevent PHP Object Injection vulnerabilities.

You may wrap an array callable within the Illuminate\Events\queueableArray function:

use App\Events\OrderCreated;
use App\Services\InventoryManager;
use function Illuminate\Events\queueableArray;
use Illuminate\Support\Facades\Event;

Event::listen(
    OrderCreated::class,
    queueableArray([InventoryManager::class, 'reserveStock'])
);

Warning: If you use queueableArray, the event payload must not contain Eloquent Models or objects. Refactor your events to pass primitive data only (e.g., passing $order->id instead of $order). This validation exception is thrown at the exact moment the event fires. For more details, see the full Maravelith Queueable Array Callables documentation.

Model Observers

You may place your Observer in the Observers folder (see \Laravel\Lumen\Providers\EventServiceProvider::discoverEventsAsObserversWithin) and set auto-discovery to true in your EventServiceProvider:

/**
* Determine if events as observers and listeners should be automatically discovered.
*/
public function shouldDiscoverEventsAsObservers(): bool
{
    return true;
}

Once enabled, the Artisan event:cache command will auto-register it; there is no need to manually call the observe method.

You can also generate observers via the CLI, see Artisan.

Note: In bootstrap/app.php, the $app->withEloquent(); MUST be placed AFTER $app->register(App\Providers\EventServiceProvider::class);. `


This site uses Just the Docs, a documentation theme for Jekyll.