【问题标题】:Laravel queueable notification errors: Serialization of 'Closure' is not allowedLaravel 可排队通知错误:不允许序列化“闭包”
【发布时间】:2017-04-26 20:07:30
【问题描述】:

我创建了一个成功运行的邮件通知,但是在尝试对其进行排队时,我收到以下错误:

Uncaught Exception: Serialization of 'Closure' is not allowed in /vendor/laravel/framework/src/Illuminate/Queue/Queue.php:125

以下是我认为导致错误的代码:

public function toMail($notifiable)
{
    $view_file = 'emails.verifyEmail';
    $view = View::make($view_file, ['invitationToken' => $this->invitationToken, 'team_name' => $this->team->name, 'team_domain' => $this->team->domain ]);

    $view = new HtmlString(with(new CssToInlineStyles)->convert($view));

    return (new MailMessage)
        ->subject('Email Verification')
        ->view('emails.htmlBlank', ['bodyContent' => $view]);
}

我不确定它试图序列化的“闭包”来自哪里。我尝试将->render() 添加到View::make 的末尾,但这似乎没有什么不同。我相信这可能与MailMessageview 功能有关,但我不太确定。

再一次,此通知在未排队时可以完美运行。

任何帮助将不胜感激。

【问题讨论】:

  • 如果您真的认为它在这些行中,请通过从给出错误的行开始跟踪,或使用步骤调试,或在每行之后使用“exit”语句来确定究竟是哪一行抛出了错误错误。然后你就可以解决问题了。
  • @PaulJeromeBordallo 我知道是哪一行导致了错误。堆栈跟踪没有帮助,因为它都指向核心 laravel 代码。我知道错误是什么,只是不知道如何解决。

标签: laravel laravel-5 laravel-queue laravel-notification


【解决方案1】:

即使这个问题已经很老了,我也会发布这个以供将来参考。

队列尝试序列化通知实例时会出现问题。这是通过序列化通知对象的每个属性来完成的。我遇到了同样的问题,因为我正在做类似的事情

public function __construct(\Exception $ex){
   $this->exception = $exception;
}

在我的通知类中。 一旦通知被包裹在 SendQueuedNotification 中,它将被 Queue 处理程序序列化。在此过程中,SendQueuedNotification 的每个属性都将被序列化,包括我们的自定义通知实例及其属性。当序列化程序尝试序列化$exception 实例时,一切都会失败;由于某种原因,异常类是不可序列化的,因为它可能在其属性中包含一个闭包。所以对我有用的是改变构造函数如下

public function __construct(\Exception $ex)
{
    $this->exceptionClass = get_class($ex);
    $this->exceptionMessage = $ex->getMessage();
    $this->exceptionLine = $ex->getFile() . '@' . $ex->getLine();
    $this->exceptionCode = $ex->getCode();
}

现在所有通知属性都完全可序列化,一切都按预期工作。

另一种解决方案是使用__wakeup()__sleep() 方法来自定义通知实例的序列化和反序列化。

希望它有助于理解您的问题。

【讨论】:

    猜你喜欢
    • 2018-05-08
    • 2019-01-30
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多