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 Name | Event Description |
---|---|
AfterRenderEvent | Occurs after the rendering process is complete. |
BeforeRenderEvent | Occurs before the rendering process starts. |
ChooseLangEvent | Occurs when a language is selected. |
LangChangedEvent | Indicates a language change. |
OnRouteFoundEvent | Occurs when a route is found in the routing system. |
ProfileRenderEvent | Related to the rendering of a user's profile. |
ResponseEvent | Related to the server response formation. |
RoutingFinishedEvent | Occurs after the routing process is complete. |
RoutingStartedEvent | Signals the start of the routing process. |
SearchEvent | Related to the search functionality. |
User Authentication and Registration Events
Event Name | Event Description |
---|---|
PasswordResetCompletedEvent | Indicates the completion of the password reset process. |
PasswordResetRequestedEvent | Indicates a request for password reset. |
SocialLoggedInEvent | Related to logging in through social networks. |
SocialProviderAddedEvent | Occurs when a new social provider is added. |
UserLoggedInEvent | Signals that a user has logged into the system. |
UserLoggedOutEvent | Signals that a user has logged out of the system. |
UserRegisteredEvent | Related to the registration of a new user. |
UserVerifiedEvent | Confirms a user's verification. |