Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hook into Spark Events on Register

I know that spark has events that can be listened to when user has registered but I'm totally new to laravel and Events, are there examples that I can make use of to access the events? My goal is to listen to the user created event and send a welcome email to the user.

like image 850
Mohammed Abrar Ahmed Avatar asked May 02 '16 05:05

Mohammed Abrar Ahmed


1 Answers

Finally, here i came up with solution.

Basically, Events call listeners that is defined in the EventServiceProvider class that is store in the providers inside the app folder of the application.

In the EventServiceProvider.php find

'Laravel\Spark\Events\Auth\UserRegistered' => [
              'Laravel\Spark\Listeners\Subscription\CreateTrialEndingNotification',
        ],

it will be store in the $listen of the EventServiceProvider class, this means that the UserRegistered event will call the CreateTrialEndingNotification listener, so we need to create a listerner and attach here , creating listener is easy just create a new file with name HookRegisteredUser(or your choice) some thing like below in app/Listeners sand add its path into the $listen of the "Laravel\Spark\Events\Auth\UserRegistered"

namespace App\Listeners;

use Laravel\Spark\Events\Auth\UserRegistered;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class HookRegisteredUser
{
    /**
     * Handle the event.
     *
     * @param  UserRegistered  $event
     * @return void
     */
    public function handle(UserRegistered $event)
    {
      //your code goes here
    }
}

After this add HookRegisteredUser listener in EventServiceProvider.php as follows,

'Laravel\Spark\Events\Auth\UserRegistered' => [
            'Laravel\Spark\Listeners\Subscription\CreateTrialEndingNotification',
            'App\Listeners\HookRegisteredUser',
        ],

Now the UserRegistered event will call two listeners i.e CreateTrialEndingNotification , HookRegisteredUser and the method handle will get executed on call to listeners and thats it!

like image 173
Mohammed Abrar Ahmed Avatar answered Oct 19 '22 01:10

Mohammed Abrar Ahmed