首页 > 解决方案 > 在 Laravel 6 中使用队列发送 ResetPassword 通知 - 错误属性“视图”

问题描述

我想覆盖 laravel 6 中密码重置和电子邮件验证的默认通知,以尽可能以最简单的方式使用队列。所以我在User.php模型中添加了方法:

use App\Notifications\ResetPasswordNotification;
use App\Notifications\EmailVerificationNotification;
...
public function sendPasswordResetNotification($token)
{
    $this->notify(new ResetPasswordNotification($token));
}

public function sendEmailVerificationNotification()
{
    $this->notify(new EmailVerificationNotification);
}

并创建新通知

重置密码通知

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Auth\Notifications\ResetPassword;

class ResetPasswordNotification extends ResetPassword implements ShouldQueue
{
    use Queueable;

}

电子邮件验证通知

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Auth\Notifications\VerifyEmail;

class EmailVerificationNotification extends VerifyEmail implements ShouldQueue
{
    use Queueable;

}

现在电子邮件验证正在发送排队,但在 url 作为主机名正在生成http://localhost/ ...在默认通知中它是正确生成的,与浏览器中的域名相同(无需在 .env 文件中更改它)。

第二个问题是密码重置通知,根本没有发送。它给了我一个错误

Trying to get property 'view' of non-object at vendor/laravel/framework/src/Illuminate/Notifications/Channels/MailChannel.php:92

而且我不明白为什么会发生并且没有按预期工作。

搜索问题我什至发现了这个(问题),fakemeta 提到它应该可以工作。

标签: notificationsqueuelaravel-6

解决方案


我想到了。首先,在使用队列时,在我的情况下,我正在运行 artisan queue:work with supervisor daemon,作业在控制台下运行,因此没有可用的 SERVER['HTTP_HOST'] var,这意味着必须从 .env 读取此值文件。其次,当您像我一样更改代码覆盖方法时,您必须重新启动此队列:工作以重新读取更改。所以这些是我的主要问题。


推荐阅读