Ad

Why Should I Use Events And Listener In Laravel

- 1 answer

I understand events and listener in laravel but I don't know what difference between events and normal functions which means if I want to send mail why I should use event while I can use normal function to do that in another word, when must I use events and when must I use functions

Ad

Answer

An Event is a way of organizing triggers for reusable logic stored elsewhere in your application.

For example, say you have an application which registers users. When a user is registered, you may wish to take certain actions such as:

  1. Email them a welcome email
  2. Update a counter in a statistics database
  3. Sign them up for a mailing list

Functional Approach

You could do this in a controller using functions like so (see the Pros and Cons under the example)

RegisterController

<?php

namespace App\Http\Controllers\Auth;

class RegisterController extends Controller
{
    ...

    protected function create(array $data)
    {
        // Create user
        $user = User::create();

        // Send Welcome Email
        $this->sendWelcomeEmail($user);

        // Update statistics database
        $this->updateStatisticsDatabase($user);

        // Add them to the mailing list
        $this->addToMailingList($user);

    }

    protected function sendWelcomeEmail(User $user)
    {
        ...
    }

    protected function updateStatisticsDatabase(User $user)
    {
        ...
    }

    protected function addToMailingList(User $user)
    {
        ...
    }

    ...
}

Pros

  • This is self contained
  • It is simple to follow

Cons

  • This is a lot of responsibility for a single controller
  • If you register users anywhere else in your application, you will need to duplicate each of the actions wherever you register users.

Event / Listener Approach

Alternatively, we can split this functionality into Events and Listeners

RegisterController

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Events\UserWasRegistered;

class RegisterController extends Controller
{
    ...

    protected function create(array $data)
    {
        // Create user
        $user = User::create();

        // Emit Event
        event(new UserWasRegistered($user));

    }

    ...
}

UserWasRegistered

<?php

namespace App\Events;

use App\User;
use Illuminate\Queue\SerializesModels;

class UserWasRegistered
{
    use SerializesModels;

    public $user;

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

SendWelcomeEmail

<?php

namespace App\Listeners;

use App\Events\UserWasRegistered;

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

    /**
     * Handle the event.
     *
     * @param  \App\Events\UserWasRegistered$event
     * @return void
     */
    public function handle(UserWasRegistered $event)
    {
        // Send the welcome email ...
    }
}

Pros

  • You now have a UserWasRegistered event which you can call from anywhere in your application.
  • The event guarantees that the same actions will happen no matter what triggers it
  • If you ever need to add new functionality, all you need to do is create a new listener and register it with the event. Job done!
  • You logic is separated and now the RegisterController has much reduced responsibility

Cons

  • If you are only ever using the Event once, you now have logic spread over several files

TLDR

On simple applications, functional programming in a single controller may be all you need. In larger more complex applications, Events and Listeners tend to be a much better option

Ad
source: stackoverflow.com
Ad