2

I have a Laravel package that fires an event which is located in the vendor folder.

The event's class that is being fired is Mikea\Kicker\Events\Interviews\SurveyWasCompleted

I need to listen in for that event and then die and dump the event's object.

I added the following code in my routes.php file

Event::listen('Mikea\Kicker\Events\Interviews\SurveyWasCompleted', function($event){
    dd($event);
});

However, nothing is displayed on the screen. I know for sure that the SurveyWasCompleted event is called because when I die and dump from inside the event I get the data.

How can I correctly listen for an event to be fired?

Junior
  • 11,602
  • 27
  • 106
  • 212

2 Answers2

3

You need to register the event in EventServiceProvider

protected $listen = [
        'Mikea\Kicker\Events\Interviews\SurveyWasCompleted' => [
            'Mikea\Kicker\Listeners\SurveyWasCompletedListener',
        ]
    ];

Then you need to create a Listener SurveyWasCompletedListener

namespace Mikea\Kicker\Listeners;

use Mikea\Kicker\Events\Interviews\SurveyWasCompleted;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class SurveyWasCompletedListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  SurveyWasCompleted $event
     * @return void
     */
    public function handle(SurveyWasCompleted $event)
    {
        //do something
    }
} 

Obviously you need to fire the event using

Event::fire(new SurveyWasCompleted($data));
rishal
  • 3,230
  • 3
  • 22
  • 29
0

You might need to register it in the EventServiceProvider rather than the routes.php file.

https://laravel.com/docs/5.2/events#registering-events-and-listeners

vonec
  • 681
  • 10
  • 22
  • I have done that too but still not working. I added a listener called `Mikea\Kicker\Listener\IncreaseStoreQuota` then I added it to the `EventsServiceProvider`. then inside the `IncreaseStoreQuota` I tried to die and dump the $event. still not working – Junior Jan 13 '16 at 00:14