Laravel

Laravel TutorialChange default routing behaviour in Laravel 5.2.31 +CustomException class in LaravelDeploy Laravel 5 App on Shared Hosting on Linux ServerLaravel ArtisanLaravel AuthenticationLaravel AuthorizationLaravel Blade TemplatesLaravel CashierLaravel CollectionsLaravel Common Issues & Quick FixesLaravel ConstantsLaravel ControllersLaravel Cron basicsLaravel Cross Domain RequestLaravel Custom Helper functionLaravel DatabaseLaravel Database MigrationsLaravel Database SeedingLaravel Directory StructureLaravel DockerLaravel EloquentLaravel Eloquent : RelationshipLaravel Eloquent: Accessors & MutatorsLaravel Eloquent: ModelLaravel Error HandlingLaravel Events and ListenersLaravel Filesystem / Cloud StorageLaravel Form Request(s)Laravel Getting started with laravel-5.3Laravel HelpersLaravel HTML and Form BuilderLaravel InstallationLaravel Installation GuideLaravel Introduction to laravel-5.2Laravel Introduction to laravel-5.3Laravel lumen frameworkLaravel Macros in Eloquent RelationshipLaravel MailLaravel MiddlewareLaravel ObserverLaravel PackagesLaravel PaginationLaravel Permissions for storageLaravel PoliciesLaravel QueuesLaravel RequestsLaravel Route Model BindingLaravel RoutingLaravel SeedingLaravel ServicesLaravel SocialiteLaravel Task SchedulingLaravel TestingLaravel Token Mismatch Error in AJAXLaravel use fields aliases in EloquentLaravel Useful linksLaravel ValetLaravel ValidationMultiple DB Connections in LaravelNaming Files when uploading with Laravel on WindowsRemove public from URL in laravelSparkpost integration with Laravel 5.4



Laravel Events and Listeners

From WikiOD

Using Event and Listeners for sending emails to a new registered user[edit | edit source]

Laravel's events allows to implement the Observer pattern. This can be used to send a welcome email to a user whenever they register on your application.

New events and listeners can be generated using the artisan command line utility after registering the event and their particular listener in App\Providers\EventServiceProvider class.

protected $listen = [
    'App\Events\NewUserRegistered' => [
        'App\Listeners\SendWelcomeEmail',
    ],
];

Alternate notation:

protected $listen = [
    \App\Events\NewUserRegistered::class => [
        \App\Listeners\SendWelcomeEmail::class,
    ],
];

Now execute php artisan generate:event. This command will generate all the corresponding events and listeners mentioned above in App\Events and App\Listeners directories respectively.

We can have multiple listeners to a single event like

protected $listen = [
    'Event' => [
        'Listner1', 'Listener2'
    ],
];

NewUserRegistered is just a wrapper class for the newly registered User model:

class NewUserRegistered extends Event
{
    use SerializesModels;

    public $user;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }
}

This Event will be handled by the SendWelcomeEmail listener:

class SendWelcomeEmail
{
    /**
     * Handle the event.
     *
     * @param  NewUserRegistered  $event
     */
    public function handle(NewUserRegistered $event)
    {
        //send the welcome email to the user
        $user = $event->user;
        Mail::send('emails.welcome', ['user' => $user], function ($message) use ($user) {
                $message->from('hi@yourdomain.com', 'John Doe');
                $message->subject('Welcome aboard '.$user->name.'!');
                $message->to($user->email);
        });
    }
}

The last step is to call/fire the event whenever a new user registers. This can be done in the controller, command or service, wherever you implement the user registration logic:

event(new NewUserRegistered($user));

Credit:Stack_Overflow_Documentation