通知

除了 Email 以外 Laravel 也有着各种各样的通知功能,这些功能整合在 Notifcation 模组中。 Notification 预设包含信件、简讯(使用 Vonage)以及 Slack 通知,需要更多能的话要引入第三方套件,社群很贴心的整理了可用的套件清单

Notifiable

如果要使用通知功能,首先要在模型中加上 Notifiable 属性,Laravel 预设建立的 User 模型就已经有使用。

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable; // Notifiable 属性
use Laravel\Sanctum\HasApiTokens;
use Illuminate\Database\Eloquent\SoftDeletes; 

class User extends Authenticatable 
{
    use HasApiTokens, HasFactory, Notifiable; // 引用属性
    
    
    //...
}

有了 Notifiable 属性,模型的实例就可以使用 notify 功能发送通知。

use App\Notifications\InvoicePaid;

$user->notify(new InvoicePaid($invoice));

notify 需要 Illuminate\Notifications\Notification 的实例作为参数,可以用指令产生继承 Illuminate\Notifications\Notification 的类别,预设放在 app/Notifications/目录下。

sail artisan make:notification VerifyNotification

app/Notifications/VerifyNotification.php

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class VerifyNotification extends Notification
{
    use Queueable;
    //...
}

产生的 Notification 预设带有 Queueable 属性,可以存入 Laravel 的伫列中易步执行,这个之後介绍。

via

via 是 Notification 类别中用於定义这则通知要经由哪些 channel 进行发送,也就是通知的管道。一则通知可以同时经由复数管道发送。

public function via($notifiable)
{
    return ['mail','database'];
}

当定义了管道,要确保有对应该管道的方法定义发送通知的方法,例如 mail 管道需要定义 toMail 方法。

public function toMail($notifiable)
{
    return (new MailMessage)
                ->line('The introduction to the notification.')
                ->action('Notification Action', url('/'))
                ->line('Thank you for using our application!');
}

上面函式都带有 $notifiable 参数,这个是指被传送通知的对象,会是带有 Notifiable 属性的 Model ,像是 User。

use App\Notifications\VerifyNotification;

$user->notify(new VerifyNotification());

像这样发送 VerifyNotification 通知的话, VerifyNotification 中的函式所带入的 $notifiable 就会是 $user

toMail

各个寄送管道对应的方法有各自必须回传的类别,以 toMail 来说就需要回传 Illuminate\Mail\Mailable 的实例,所以也可以用我们自制的信件类别。

public function toMail($notifiable)
{
    return new CustomVerifyMail;
}

其他第三方套件也都有类似的函式,像是 AWS SNS 需要定义 toSns 函式。

toArray / toDatabase

database 通知管道用於将通知纪录於资料库中,对应的函式用 toArraytoDatabase 都可以。

public function toArray($notifiable)
{
    return [
        'invoice_id' => $this->invoice->id,
        'amount' => $this->invoice->amount,
    ];
}

不过在使用之前记得先建好资料表单。

php artisan notifications:table

php artisan migrate

自订管道

除了预设的通知管道像 maildatabase 外,也可以自定义管道,各套件也都有定义。

例如 AWS SNSSnsChannel

<?php

use NotificationChannels\AwsSns\SnsChannel;
use NotificationChannels\AwsSns\SnsMessage;
use Illuminate\Notifications\Notification;

class AccountApproved extends Notification
{
    public function via($notifiable)
    {
        return [SnsChannel::class];
    }
    
    //...
        
}

自己撰写 Channel 的话,要包含有 send 函式。

<?php

namespace App\Channels;

use Illuminate\Notifications\Notification;

class VoiceChannel
{
    public function send($notifiable, Notification $notification)
    {
        $message = $notification->toVoice($notifiable);

    }
}

send 需要两个参数,第一个 $$notifiable 跟前面一样是传送的对象,也是经由 Notification 类别底下的 via 传入的。 另一个 $notification 就是 Notification 物件本身,所以可以在 Notification 底下定义传送的方式,就像 toMailtoSns 等,然後在这里使用。


<<:  拥抱组合叠叠乐 Composition API [序]

>>:  Day28 如果用AR养宠物 会不会减少很多弃养的问题?

同步与非同步

刚开始学习JavaScript的时候,很单纯的认为所有程序码是逐行执行的,就像看书不都是ㄧ行一行阅读...

[Day 12] -『 GO语言学习笔记』- for range 回圈

以下笔记摘录自『 The Go Workshop 』。 Go语言只支援一种回圈回圈叙述,就是for回...

[Day10]关於创世区块block#0

什麽是创世区块?在第二天{什麽是区块链}一文中有提到中聪在2009年时,创建了比特币网路并开发了第...

Nvidia Docker安装说明(含WSL2)

订阅patreon即可看到更多文章 https://www.patreon.com/wade3c ...

铁人赛 Day5 -- 一定要知道的 CSS (二) - display属性的应用

前言 不罗嗦,一样先附上程序码 <!DOCTYPE html> <html lan...