artisan / laravel-semaphore
2.0.1
2019-03-21 19:47 UTC
Requires
- php: >=7.0
- kitetail/zttp: ^0.4.0
This package is auto-updated.
Last update: 2020-02-13 09:05:16 UTC
README
Semaphore 是一家菲律宾短信服务提供商。
安装
composer require artisan/laravel-semaphore
如果您正在使用 Laravel 5.5 的包发现,则无需添加服务提供者。
否则,在您的 config/app.php 中添加 Semaphore 服务提供者
return [ 'providers' => [ // ... Artisan\Semaphore\ServiceProvider::class, ], ]
配置
在您的 .env 文件中,复制以下默认模板,然后您可以添加您的 Semaphore API 密钥和发送者名称。
SEMAPHORE_KEY=
SEMAPHORE_FROM_NAME=
如果您想自定义配置文件,可以通过以下方式发布配置文件:
php artisan vendor:publish --tags=semaphore
使用方法
创建通知后,我们可以开始使用 Aritsan\Semaphore\SemaphoreChannel 来发送 SMS。
您应该在通知类上定义一个 toSemaphore 方法。此方法将接收一个 $notifiable 实体,并应返回一个 Artisan\Semaphore\SemahporeMessage 实例
use Artisan\Semaphore\SemaphoreChannel; use Artisan\Semaphore\SemaphoreMessage; class ReminderMessage extends notification { public function via($notifiable) { return ['slack', SemaphoreChannel::class]; } public function toSemaphore($notifiable) { return (new SemaphoreMessage) ->content("Hey {$notifiable->name}, don't forget to brush your teeth!"); } }
如果您希望使用与在您的 config/semaphore.php 文件中指定的名称不同的名称发送通知,您可以在 SemaphoreMessage 实例上使用 from 方法
public function toSemaphore($notifiable) { return (new SemaphoreMessage) ->content("Hey {$notifiable->name}, don't forget to brush your teeth!") ->from('Artisan'); }
通过 SemaphoreChannel::class 发送通知时,通知系统 不会 自动在可通知条目上查找任何属性。要指定通知要发送到的哪个号码,请在实体上定义一个 routeNotificationForSemaphore 方法
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * Route notifications for the Semaphore channel. * * @param \Illuminate\Notifications\Notification $notification * @return string */ public function routeNotificationForSemaphore($notification) { return $this->mobile; } }