kagatan / sms-fly
通过sms-fly发送短信
dev-master
2021-11-20 10:48 UTC
This package is auto-updated.
Last update: 2024-09-20 17:37:46 UTC
README
“SMS-fly”服务的通知频道
使用此包,您可以将短信通知轻松集成到您的Laravel应用程序中,使用的是SMS-fly服务。
安装
在终端中执行以下命令以安装此包
$ composer require kagatan/sms-fly
此操作需要已安装的
composer
。要安装它,请访问此链接。
如果您使用的是Laravel 5.5及以上版本,则此包的服务提供程序将自动注册。否则,您需要手动在文件./config/app.php
的providers
部分中注册服务提供程序
'providers' => [ // ... Kagatan\SmsFly\SmsFlyServiceProvider::class, ]
添加门面
'aliases' => [ ... 'SmsFly' => Kagatan\SmsFly\Facades\SmsFly::class ]
在文件config/services.php
中添加
// config/services.php ... 'sms-fly' => [ 'login' => function_exists('env') ? env('SMSFLY_LOGIN', '') : '', 'password' => function_exists('env') ? env('SMSFLY_PASSWORD', '') : '', 'from' => function_exists('env') ? env('SMSFLY_FROM', '') : '', ], ...
发布提供程序
php artisan vendor:publish --provider="Kagatan\SmsFly\SmsFlyServiceProvider"
配置
安装后,您需要更改文件./.env
,并添加以下键值对
SMSFLY_LOGIN=xxxxx SMSFLY_PASSWORD=xxxxx SMSFLY_FROM=SENDER-NAME
升级
composer update kagatan/sms-fly
使用
使用Laravel应用程序中的通知功能发送SMS通知的基本示例
SmsFlyMessage对象可用的方法
通知类示例
<?php use Illuminate\Notifications\Notification; use Kagatan\SmsFly\SmsFlyChannel; use Kagatan\SmsFly\SmsFlyMessage; /** * Notification object. */ class InvoicePaid extends Notification { /** * Get the notification channels. * * @param mixed $notifiable * * @return array|string */ public function via($notifiable) { return [SmsFlyChannel::class]; } /** * Get the SMS Fly Message representation of the notification. * * @param mixed $notifiable * * @return SmsFlyMessage */ public function toSmsFly($notifiable) { return SmsFlyMessage::create() ->content('Some SMS notification message'); } }
在您可通知的模型中,必须添加routeNotificationForSmsFly()
方法,该方法返回电话号码或电话号码数组。
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Illuminate\Notifications\Notifiable; class User extends Model { use Notifiable; /** * Route notifications for the SmsFly channel. * * @param $notifiable * @return string */ public function routeNotificationForSmsFly($notifiable) { return $this->phone; } }
使用Notifiable特性示例
$user->notify(new InvoicePaid());
使用Notification门面示例
Notification::send($users, new InvoicePaid());
使用门面发送SMS示例(不使用Notification)
<?php use Kagatan\SmsFly\Facades\SmsFly; use Kagatan\SmsFly\SmsFlyMessage; public function test(){ $message = SmsFlyMessage::create() ->content("Example sending SMS.") ->to("380987654210") ->from("WiFi-POINT") ->toArray(); $id = SmsFly::send($message); $errors = SmsFly::getErrors(); dd($id, $errors); }
许可
本包代码在MIT许可下发布。