gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 03:13:58 25 4
gpt4 key购买 nike

我已经阅读了很多关于 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com