How to write code in laravel event monitoring is more reasonable

using the event listening function of laravel for the first time, if you want to implement the following business logic, please tell me how to write reasonable code!

//controller codes
class OrderController extends Controller
{
    function create()
    {
        //do something ...
        
        
        event(new OrderWasCreated($order_id));
    }
}

//api codes
class ApiController extends Controller
{
    function sendEmail()
    {
        //do something ...
    }
}

//Listeners codes
class SendEmail
{
    public function handle(OrderWasCreated $event)
    {
        //send email ...
    }
}

like the above three pieces of code, now I want to implement the business logic of sending mail in the listener when the order is created, but I"m not sure where to write the code to handle the mail. So I created an api to deal with, how should it be associated with the listener?

Mar.23,2021

usually you can send email in Listeners. If you want to use api, you can use api as the global helper of , or use.. / Api,. Then in the SendEmail handle method new Api () call


I think ApiController is redundant
SendEmail listener itself is to send the order generated mail basically does not need to be reused.
at most encapsulates a private method in the SendEmail class to handle sending


can be sent directly in listener. Take enough data from the event object and throw it into the queue for background execution.


if it were me, I would do this

first create an event of OrderCreated

create another OrderCreatedListener and listen for OrderCreated event

create another OrderCreatedNotification to handle notification of order creation

finally create a OrderCreatedMail to generate notification messages

the calling method is that the controller triggers the OrderCreated event, OrderCreatedListener listens to the event, triggers OrderCreatedNotification , OrderCreatedNotification then calls OrderCreateMail to generate notification email, and finally sends

benefits of this

  1. notifications are decoupled from listeners. Listeners can not only do notification functions, but can also do other functions such as order timeout closing (of course, you can also create an additional Listener processing).
  2. notifications are very easy to extend. In the later stage, it is very convenient to expand the notification channel, such as App notification, SMS notification and so on.
  3. Mail is decoupled from notifications. In the notification, you can call different notification emails according to the user type. For example, for VIP users and ordinary users, call different Mail class templates to generate different Mail .

the subject owner can take corresponding steps to increase or decrease according to the complexity of his or her business

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-16ded69-892e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-16ded69-892e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?