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:

The --method flag in the above comment is optional, and if not provided, the handler will register with the handle method.

Even the --event is also optional but we strongly advise to always provide it, because when it's not provided, yuga-auto-events will be the registered event

If the provided event name is a valid php class, the class will be injected in the event method provided as below.

For the following command

And the event class should be as below:

In which case you can no longer dispatch the event like so

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:

Last updated

Was this helpful?