Laravel 广播:通知与事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39314809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:24:36  来源:igfitidea点击:

Laravel Broadcasting: Notification vs Event

phplaravel

提问by Pezhvak

I read laravel documentations about Eventsand Notifications, it seems we can fire an event and from that event (using ShouldBroadcastinterface) broadcast it to laravel echo which i understand, in the other hand we can use Notifications viaBroadcastto do the same, so what's the difference?

我阅读了关于Eventsand 的laravel 文档Notifications,似乎我们可以触发一个事件,然后从该事件(使用ShouldBroadcast接口)将它广播到我理解的 laravel echo,另一方面,我们可以使用通知viaBroadcast来做同样的事情,那么有什么区别呢?

采纳答案by Pezhvak

After thinking a lot, i found out that they are made for different things, here's what i understood:

经过深思熟虑,我发现它们是为不同的东西而制造的,这是我的理解:

Notifications:

通知:

Consider facebook, everytime you login you see bunch of notifications about things that happened while you where away, also if you are present you see live notifications..

想想 facebook,每次登录时,您都会看到一堆关于在您离开时发生的事情的通知,如果您在场,您也会看到实时通知。

meanwhile you're getting emails about notifications that you want.. this is exactly what Laravel Notifications is doing. you can use notifymethod on your eloquent models such as App\Userabout something like OrderApprovedwhich will do whatever you planned it to do for you like sending sms to that user. and also you can save one instant of that notification on database so when user comes back he or she can see that you have approved their order..

同时,您会收到有关您想要的通知的电子邮件。这正是 Laravel Notifications 所做的。你可以notify在你的雄辩模型上使用方法,比如类似的App\User东西OrderApproved,它会做你计划为你做的任何事情,比如向该用户发送短信。并且您还可以在数据库中保存该通知的一瞬间,以便当用户回来时,他或她可以看到您已批准他们的订单..

Events:

事件:

it's when something happens, like when a new user is created and you want to do different things like sending verification email, sending verification sms and.. this is why you create an event so that you could handle different logics of that event using listeners. when it comes to broadcasting, you can use ShouldBroadcastinterface on your event and from there you can sync data with your admin panel that a new user is registered. this will be useful when admin is watching list of users and without reloading the page you could user Laravel Echoto receive that event on admin panel and append new registered user to the list.

这是发生某些事情的时候,比如当一个新用户被创建时,你想要做不同的事情,比如发送验证电子邮件,发送验证短信等等。这就是为什么你创建一个事件,以便你可以使用侦听器处理该事件的不同逻辑。在广播方面,您可以ShouldBroadcast在活动中使用界面,从那里您可以与注册新用户的管理面板同步数据。当管理员正在监视用户列表并且无需重新加载页面时,这将很有用,您可以Laravel Echo在管理面板上接收该事件并将新注册用户附加到列表中。

Conclusion:

结论:

it really depends on what you need, if you just want to update something in your interface, maybe events are what you need. but if you need to do more you can use notifications.

这实际上取决于您的需求,如果您只想更新界面中的某些内容,也许事件就是您所需要的。但如果你需要做更多的事情,你可以使用通知。

in the end events are used when you need to do things when something happens while notifications are report of what just happened.

最后,当您需要在发生某些事情时做某事时使用事件,而通知则报告刚刚发生的事情。

hope it help others..

希望它能帮助别人..

回答by Christophvh

What the provided answer lacks imo is that they are in most cases used both instead of 1 or the other, which seems to be the tone of the provided answer/question.

提供的答案缺乏 imo 的是,在大多数情况下,它们同时使用,而不是 1 或另一个,这似乎是所提供的答案/问题的语气。

An event is something significant in your application. Let's assume your application is a Webshop.

事件在您的应用程序中具有重要意义。让我们假设您的应用程序是一个网上商店。

A significant action in your webshop can be Product Purchased. When a product is purchased you need to do a lot of different steps. Putting this all inside a controller and potentially in several different places can get very messy and not clear.

您的网上商店中的一项重要操作可以是“购买产品”。购买产品后,您需要执行许多不同的步骤。把这一切都放在一个控制器中,可能放在几个不同的地方,可能会变得非常混乱和不清楚。

So a good approach would be to use a Eventcalled ProductPurchased. This event can have Listeners , those listeners are in this case all the steps you need to preform when a user purchases a product.

所以一个好的方法是使用一个名为ProductPurchased事件。此事件可以有 Listeners ,在这种情况下,这些侦听器是用户购买产品时需要执行的所有步骤。

e.g.: ProductPurchased (event)

例如:ProductPurchased(事件)

  • BillClient (eventlistener)
  • GenerateInvoice (eventlistener)
  • notifyClient (eventlistener)
  • ...
  • BillClient(事件监听器)
  • 生成发票(事件监听器)
  • 通知客户端(事件监听器)
  • ...

Let's say we want to notify our client with a text-message and an email when they purchased a product.

假设我们想在客户购买产品时通过短信和电子邮件通知他们。

So on the notifyClient event-listener we can create a Notification. This notification is responsible for sending a message to the client. This can be a SMS/Slack-message/Email/...

所以在 notifyClient 事件监听器上我们可以创建一个Notification。此通知负责向客户端发送消息。这可以是 SMS/Slack-message/Email/...

And like you mentioned both Events and Notifications can be put on the Queue or can be broadcasted.

就像你提到的,事件和通知都可以放在队列中或可以广播。

You choose notifications when you want to send something to different channels. Mail/SMS/Slack.. If you only need broadcasting you can just use ShouldBroadcast. Just like when you only want to send a e-mail use Mail:: without the need for a notification.

当您想向不同渠道发送内容时,您可以选择通知。邮件/短信/Slack .. 如果您只需要广播,您可以使用 ShouldBroadcast。就像当您只想发送电子邮件时使用 Mail:: 而无需通知。

Notifications are a nice way to group the same 'message' to different destinations.

通知是将相同的“消息”分组到不同目的地的好方法。