How to create Event for Mail sending in Laravel 5.2? (ok)
https://www.itsolutionstuff.com/post/how-to-create-event-for-mail-sending-in-laravel-52example.html
C:\xampp\htdocs\reset\app\Providers\EventServiceProvider.php
Sử dụng lập trức nó tại ra 2 file tương ứng trong Events, Listeners
C:\xampp\htdocs\reset\app\Events\LoginHistory.php
C:\xampp\htdocs\reset\app\Listeners\storeUserLoginHistory.php
C:\xampp\htdocs\reset\app\Http\Controllers\Auth\LoginController.php
Laravel 8 Events and Listeners with Practical Example
1Laravel 8 Events and Listeners with Practical Example2Schedule a task to run at a specific time in laravel (CronJob)
Hello, today I want to talk about Events and Listeners in Laravel, in programming to write a scalable, reusable and clean code, you need to follow some programming principles, one of which is SOLID, I will not be going deep into explaining that today, but I will just highlight one, the S which stands for Single Responsibility Principle, this principle states that
A class should have one and only one reason to change, meaning that a class should have only one job.
What this means is that, A class should only perform one task, many times, we always load our class with some many functionalities, so if the class changes, a lot of things will break in our application, which is not a good practice. So I will introduce Events and Listeners to help in making our class perform just one task.
Click on my profile to follow me and get more updates.
What is An Event?
Events are the ways we hook into the activities of our application, it is just a way to observe an activity, for instance, login, a class can be created to monitor the activity of login, when a user logs in, the event class can execute some functions.
What is A Listener?
A Listener is a class that listens to the events that they are mapped to and execute a task, that is they are the ones that perform a given task for an event. Let me illustrate, you might want to send a welcome email to a new user of your application, and also assign a role to the user based on the information provided in the registration, etc, you would not want to do all those in the RegisterController because that we violate the first principle of SOLID, where the Controller will perform more than one task, the RegisterController needs to only perform the activity of registration of a new user. so an event needs to be carried out in the process of registration, where assigning a role, sending an email, etc are the individual listeners under the event. For this article, I will write one listener in an event, what the listener will do is to store the login of each user of the app in a table, this is just an illustration that will show you how it works. If you have a laravel project that has auth already, you can follow immediately, or you can follow my article Basic Laravel Login and Registration using Laravel Breeze, I will be using the project on that article from my system.
Step 1: Register the event and listeners in the EventServiceProvider
We added another event class called LoginHistory and also a listener called StoreUserLoginHistory, and noticed up that we called the class here in this way use App\Events\LoginHistory; and use App\Listeners\storeUserLoginHistory;, don't worry, I know you are wondering that the class does not exist in our application, we are going to generate it in the next step, you can add as many events and listeners as possible like this and even more
Step 2: Generate Events & Listeners
Previously, we write classes of events and listeners in the EventServiceProvider, so to generate it at once, we run this command
php artisan event:generate
Step 3: Write the Events and Listener class
Remember what we are trying to achieve was to store all the login of our app in a table, so click on the app/Events/LoginHistory.php and edit as follows
from the code above, the event accepts the $user which is the information of the user, and it will pass it to the listener. Click on app/Listeners/storeUserLoginHistory.php, this is where we are going to be writing the main logic of the storing of the login history, inside the handle method, add the following code
Also remember to call the Carbon and DB facade before the class
Step 4: Create the Table and Migrate
Step 5: Dispatch the Event
This is the last step, we need to call the event in the LoginController, if you are using laravel 7, or below, you can create a method in the LoginController.php like this
but for this article, I am using Laravel Breeze, a simple UI scaffolding that was released 16 days ago, so I will go to LoginRequest.php found in app/Http/Requests/Auth/LoginRequest.php and inside the authenticate() method, I will call my event and pass the $user to the class
Follow me for more of my articles, you can leave comments, suggestions, and reactions. I am open to any vacancy as a PHP backend engineer, my strength is in the Laravel framework
click the link to view my profile and follow me
How to create Event for Mail sending in Laravel 5.2?
Ok, now you can see file in this path app/Events/SendMail.php and put bellow code in that file.
app/Events/SendMail.php
Next, we need to create event listener for "SendMail" event. So create event listener using bellow command.
In this event listener we have to handle event code, i mean code of mail sending, Before this file you have to check your mail configration, If you did not set then you can set this way :How to set gmail configration for mail in Laravel?.
Then you have to put bellow code on app/Listeners/SendMailFired.php.
app/Listeners/SendMailFired.php
Now we require to register event on EventServiceProvider.php file so, open app/Providers/EventServiceProvider.php and copy this code and put in your file.
app/Providers/EventServiceProvider.php
At Last we are ready to use event in our controller file. so use this way:
app/Http/Controllers/HomeController.php
Read Also: Laravel Mailchimp api integration from scratch with example
Ok Now check in your application,
Happy Code......
Last updated
Was this helpful?