[英]Any way to dispatch a closure in laravel 5?
In laravel 4, I could push a closure onto the queue with queue::push(function...)
, but this no longer works in laravel 5. Instead, it appears that I have to make a custom Job class for every function that I want to push onto the queue.在 Laravel 4 中,我可以使用queue::push(function...)
将闭包推送到队列上,但这在 Laravel 5 中不再有效。相反,似乎我必须为每个函数创建一个自定义 Job 类我想加入队列。
Since the functions I want to be pushing are only a couple of lines long, and are only ever used in exactly one place, it really seems like a waste of time and space to be writing up a full class for every case.由于我想要推送的函数只有几行长,并且只在一个地方使用过,因此为每个案例编写一个完整的类似乎真的是浪费时间和空间。
The best "solutions" I can currently think of, are to either have a helper function that uses PHP's reflection methods to dynamically generate a new class when called, or to have generic job that accepts a closure as parameter, ie dispatch(new ClosureJob(function(){...}));
我目前能想到的最好的“解决方案”是使用 PHP 的反射方法在调用时动态生成新类的辅助函数,或者具有接受闭包作为参数的通用作业,即dispatch(new ClosureJob(function(){...}));
These seem less than ideal to me.这些对我来说似乎不太理想。 Is there another way to do this?有没有另一种方法可以做到这一点? Or am I going to have to implement one of these?或者我将不得不实施其中之一?
I've accomplished this by relying on the OpisClosure library.我依靠OpisClosure库实现了这一点。 Extend the class as so:像这样扩展类:
class QueueableClosure extends SerializableClosure
{
public function handle() {
call_user_func_array($this->closure, func_get_args());
}
}
Then use it like this:然后像这样使用它:
Queue::push(new QueueableClosure(function(){
Log::debug("this is the QueueableClosure in action.");
}));
NB See the comment below from @Quezler about possible limitations!请注意,请参阅@Quezler 下面关于可能的限制的评论!
As of Laravel v5.7 you can queue an closure like this:从 Laravel v5.7 开始,您可以像这样对闭包进行排队:
$podcast = App\Podcast::find(1);
dispatch(function () use ($podcast) {
$podcast->publish();
});
Docs: https://laravel.com/docs/7.x/queues#queueing-closures文档: https : //laravel.com/docs/7.x/queues#queueing-closures
However it is strongly recommended to use a dedicated job class to improve your code quality and for the sake of a better maintenance of your application.但是,强烈建议使用专用作业类来提高代码质量并更好地维护应用程序。 Think of you want to check what tasks are left in queue, or want to control on which queue/connection the particular code should run at.想想你想检查哪些任务留在队列中,或者想控制特定代码应该在哪个队列/连接上运行。
Therefore you need a dedicated job class: https://laravel.com/docs/5.7/queues#creating-jobs因此你需要一个专门的工作类: https : //laravel.com/docs/5.7/queues#creating-jobs
I'd say that writing a dedicated class is pretty much the Laravel standard and this is what you should align to.我会说编写一个专门的类几乎是 Laravel 的标准,这也是你应该遵循的。
https://laravel.com/docs/5.0/queues#queueing-closures says: https://laravel.com/docs/5.0/queues#queueing-closures说:
You may also push a Closure onto the queue.您也可以将闭包推送到队列中。 This is very convenient for quick, simple tasks that need to be queued:这对于需要排队的快速、简单的任务来说非常方便:
Pushing A Closure Onto The Queue将闭包推入队列
Queue::push(function($job) use ($id)
{
Account::delete($id);
$job->delete();
});
However, my guess is that you're using Laravel 5.3+ , because https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0 says:但是,我的猜测是您使用的是 Laravel 5.3+ ,因为https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0说:
Queueing Closures is no longer supported.不再支持排队闭包。 If you are queueing a Closure in your application, you should convert the Closure to a class and queue an instance of the class.如果您在应用程序中排队一个闭包,您应该将闭包转换为一个类并将该类的一个实例排队。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.