Yuga Framework
  • Yuga Framework
  • Routing
  • Middleware
  • Controllers
  • ViewModels
  • Database
    • Elegant
      • Relationships
      • Complex queries
      • Events
    • Query
    • Migrations
  • Events
    • Event Classes
    • Event Handlers
  • Service Providers
  • Views
    • MVC
      • Hax Templates
    • MVVM
  • Helpers
  • Schedulers
  • Queue worker
  • Commands
    • Scaffold
Powered by GitBook
On this page

Was this helpful?

  1. Events

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
sAuthentic

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

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());

PreviousEvent ClassesNextService Providers

Last updated 5 years ago

Was this helpful?