Event Handlers
Event handlers are classes that carry the logic of the events defined in previous sections
Defining Handlers
<?php
/**
* This file was auto-generated.
*/
namespace App\Handlers;
use Yuga\EventHandlers\HandlerInterface;
class WhenAuthenticated implements HandlerInterface
{
/**
* Event Handler Logic here
* @param \Yuga\Events\Dispatcher $event
* @return mixed
*/
public function handle($event)
{
return null;
}
/**
* Your Event Handler Logic here
* @param \Yuga\Events\Dispatcher $event
* @return mixed
*/
public function isAuthentic($event)
{
return null;
}
}The above code is a result of the:
php yuga event:handler WhenAuthenticated --event=on:userauthenticated --method=i
sAuthenticThe
--methodflag in the above comment is optional, and if not provided, the handler will register with thehandlemethod.Even the
--eventis also optional but we strongly advise to always provide it, because when it's not provided, yuga-auto-events will be the registered event
For the following command
php yuga event:handler WhenAuthenticated --event=UserAuthenticated --method=i
sAuthentic<?php
/**
* This file was auto-generated.
*/
namespace App\Handlers;
use App\Events\Test;
use Yuga\EventHandlers\HandlerInterface;
class WhenAuthenticated implements HandlerInterface
{
/**
* Event Handler Logic here
* @param \Yuga\Events\Dispatcher $event
* @return mixed
*/
public function handle($event)
{
return null;
}
/**
* Your Event Handler Logic here
* @param App\Events\UserAuthenticated $event
* @return mixed
*/
public function isAuthentic(UserAuthenticated $event)
{
return null;
}
}And the event class should be as below:
<?php
namespace App\Events;
use Yuga\Events\Dispatcher\Dispatcher;
class UserAuthenticated extends Dispatcher
{
/**
* Event Name
*/
protected $name = 'on:userauthenticated';
}In which case you can no longer dispatch the event like so
event('on:userauthenticated');inside of any controller or view-model for that matter, this is because of the contract provided in the event handler, so we are left with the option of:
event(new \App\Events\UserAuthenticated());Last updated
Was this helpful?