Why Should I Use Events And Listener In Laravel
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
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:
- Email them a welcome email
- Update a counter in a statistics database
- 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 Event
s and Listener
s
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, Event
s and Listener
s tend to be a much better option
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?