Performance: Event dispatching
Event dispatching affects server response time and user experience by controlling how quickly events trigger actions and update the UI.
<?php Event::dispatch('user.action', ['actions' => ['registered', 'logged_in', 'profile_updated']]); ?>
<?php Event::dispatch('user.registered'); Event::dispatch('user.logged_in'); Event::dispatch('user.profile_updated'); ?>
| Pattern | Listeners Triggered | Server CPU Load | Response Delay | Verdict |
|---|---|---|---|---|
| Multiple separate dispatches | Many | High | Longer | [X] Bad |
| Single combined dispatch | One | Low | Shorter | [OK] Good |
UserRegistered with a user object $user?event() helper with a new event instance.event(new UserRegistered($user)); matches Laravel's pattern.class OrderPlaced {
public $orderId;
public function __construct($orderId) {
$this->orderId = $orderId;
}
}
// Dispatch event
event(new OrderPlaced(123));
// Listener logs the order ID
Log::info('Order placed with ID: ' . 123);OrderPlaced is dispatched with order ID 123, and the listener logs this ID.event(UserRegistered($user));
new.UserRegistered($user) like a function, missing new.ProfileUpdated only if the user's email has changed. Which code correctly implements this conditional event dispatch?isDirty('email') method checks if the email attribute changed.Pass the test and this pattern moves unknown