events - 如何在 laravel 中使用 Event::queue?

标签 events laravel queue

我已经阅读了很多关于 Event::queue 的内容但我无法理解它,所以我有类似的东西:

Event::listen('send_notification');

在我使用的 Controller 中

Event::fire('send_notification');

但是因为这需要一些时间才能将用户发送到其他地方,所以我想使用

Event::queue('send_notification');

在用户被重定向后触发事件,但我不知道如何。

(在 app/config/app.php 中,我将 queue driver 设置为 sync )

编辑:

关于触发事件的小提示,您可以像往常一样完成所有工作,并添加所有 Event::flush()作为过滤器,然后通过 ->after() 调用该过滤器或 afterFilter() .

最佳答案

首先,让我澄清一点。 Event::queueQueue facade 和配置中的查询驱动程序无关。它不会让您在请求发生后触发事件。

但是您可以延迟事件的触发并因此“准备”它。

用法非常基础。显然,您需要一个或多个 Event::listen(没有它们也能正常工作,但完全没有意义)

Event::listen('send_notification', function($text){
    // send notification
});

现在我们对事件进行排队:

Event::queue('send_notification', array('Hello World'));

最后,通过调用 flush

来触发它
Event::flush('send_notification');

在您的评论中,您询问了一次刷新多个事件。不幸的是,这实际上是不可能的。你必须多次调用 flush()

Event::flush('send_notification');
Event::flush('foo');
Event::flush('bar');

如果您有很多事件要刷新,您可能需要考虑您的架构,以及是否可以将其中一些事件组合到一个具有多个监听器的事件中。

重定向后刷新事件

Event::queue 不能用于在请求生命周期结束后触发事件。你必须使用 "real" queues为此。

关于events - 如何在 laravel 中使用 Event::queue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27690926/

相关文章:

php - 如何获取 Laravel 中特定表自动生成字段的下一个 id?

C# 类设计 - 事件和长时间运行的方法

events - Blazor 组件 : How to communicate from grandchild to child to parent or grandchild to parent

javascript - 多个元素的事件监听器,当存储为变量时

javascript - 如何将数据从vue组件传递到laravel中的 View (HTML)

Java Azure ServiceBusService listQueues() 上限为 100 个队列?

events - 离散事件模拟期间使事件出队

Laravel 4.2 子域路由参数

java - 如何设置 Java DelayQueue 的容量

ajax - 如何 : Get real-time notifications in Laravel 4 using Iron. io MQ、推送队列和 AJAX