Skip to main content

Events

Flute features a system for handling events.

To listen to events, you can use the EventDispatcher class or the events() helper.

How to Listen to Events

To listen to events, you need to invoke the EventDispatcher, provide a handler, and set up a listener:

events()->addListener(ResponseEvent::NAME, [
SomeHandler::class,
'onResponse'
]);

In this example, we're listening to Response render events and calling the onResponse method in the SomeHandler class, where the argument will be the ResponseEvent itself:

class SomeHandler
{
public function onResponse(ResponseEvent $event)
{
dd($event); // Inspect the content of Response
}
}
warning

The listener method must be public, otherwise, an error will occur!

How to Create Events

Below is an example of a custom event:


use Symfony\Contracts\EventDispatcher\Event;

class SomeEvent extends Event
{
public const NAME = 'event.name';

protected ?string $arg;

public function __construct(string $arg)
{
$this->arg = $arg;
}

public function getArg(): ?string
{
return $this->arg;
}
}

We've created a class, defined a public NAME, and then created arbitrary methods.

Methods can be absolutely any kind.

To dispatch our event, it's enough to call our event like this:

$event = new SomeEvent('Events works!');

events()->dispatch($event, SomeEvent::NAME);

List of Events

Main Rendering and Routing Events

Event NameEvent Description
AfterRenderEventOccurs after the rendering process is complete.
BeforeRenderEventOccurs before the rendering process starts.
ChooseLangEventOccurs when a language is selected.
LangChangedEventIndicates a language change.
OnRouteFoundEventOccurs when a route is found in the routing system.
ProfileRenderEventRelated to the rendering of a user's profile.
ResponseEventRelated to the server response formation.
RoutingFinishedEventOccurs after the routing process is complete.
RoutingStartedEventSignals the start of the routing process.
SearchEventRelated to the search functionality.

User Authentication and Registration Events

Event NameEvent Description
PasswordResetCompletedEventIndicates the completion of the password reset process.
PasswordResetRequestedEventIndicates a request for password reset.
SocialLoggedInEventRelated to logging in through social networks.
SocialProviderAddedEventOccurs when a new social provider is added.
UserLoggedInEventSignals that a user has logged into the system.
UserLoggedOutEventSignals that a user has logged out of the system.
UserRegisteredEventRelated to the registration of a new user.
UserVerifiedEventConfirms a user's verification.