Laravel 队列不允许“闭包"的序列化
问题描述
我需要帮助以使用 Laravel 调度作业,似乎系统在使用队列作业时尝试序列化闭包,因此出现此错误.
I need help to dispatch a job with Laravel, seems the system try to serialize a closure when using queue job so there's this error.
我该如何解决这个问题?我也试过这个包
https://github.com/jeremeamia/super_closure
但在我的情况下它不起作用.
How can I fix this problem? I also tried this package
https://github.com/jeremeamia/super_closure
but it doesn't work in my case.
这是我的代码:
在控制器 FacebookController 中:
In the controller FacebookController:
还有要派遣的工作:
似乎错误出现在 $this->fb = $fb;
//'Closure' 的序列化的构造函数上
It seems the error is on the constructor at $this->fb = $fb;
// Serialization of 'Closure' is not allowed
推荐答案
我的理解是不能在 Job 中包含不可序列化的变量,因为 Laravel 在存储 Job 详细信息时不知道如何处理它们.
My understanding is that you can't include non-serializable variables in the Job, as Laravel won't know how to deal with them when storing the job details.
所以你需要从 construct()
方法和关联的 protected $fb;
和 $ 中删除
行.$fb
this->fb = $fb;
So you need to remove the $fb
from the construct()
method and the associated protected $fb;
and $this->fb = $fb;
lines.
相反,您有两个选择
1) 简单
在您的 handle()
方法中创建一个新的 SammyKLaravelFacebookSdkLaravelFacebookSdk
对象,例如
Create a new SammyKLaravelFacebookSdkLaravelFacebookSdk
object in your handle()
method, e.g.
2) 高级
如果需要传递之前使用的具体FB实例,注册一个FacebookSDK
Service Provider 和 将其注入处理程序,如文档.
If you need to pass the specific FB instance you used before, register a FacebookSDK
Service Provider and inject it into the handler as seen in the docs.
例如
当您为 Facebook SDK 注册绑定服务容器时,handle()
方法中的这种类型提示会自动检索 $fb 实例并将其注入到可供使用的方法中.您可以确定是每次都创建一个新的 FacebookSDK 对象,还是只创建一个并始终使用该对象(称为 Singleton).
When you register bind the service container for your Facebook SDK, this type-hinting in the handle()
method automatically retrieves the $fb instance and injects it into the method ready for use. You can determine whether to create a new FacebookSDK object each time, or just create one and always use that one (known as a Singleton).
这篇关于Laravel 队列不允许“闭包"的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!