jlorente / laravel-esendex
Laravel 5.6 对 Esendex SDK 的集成。
Requires
- php: >=7.3
- esendex/sdk: ^3.0
- illuminate/support: >=5.5
README
包括通知通道的 Esendex SDK 的 Laravel 集成。
安装
安装此扩展的首选方式是通过 Composer。
安装 Composer 后,您可以使用以下命令安装扩展
$ php composer.phar require jlorente/laravel-esendex
或者添加
... "require": { "jlorente/laravel-esendex": "*" }
到您的 composer.json
文件的 require
部分。
配置
- 在您的
config/app.php
服务提供者列表中注册 ServiceProvider。
config/app.php
return [ //other stuff 'providers' => [ //other stuff \Jlorente\Laravel\Esendex\EsendexServiceProvider::class, ]; ];
- 如果您愿意,可以将以下 facade 添加到 $aliases 部分。
config/app.php
return [ //other stuff 'aliases' => [ //other stuff 'Esendex' => \Jlorente\Laravel\Esendex\Facades\Esendex::class, ]; ];
- 在
config/services.php
文件中设置您的 esendex 账户的引用、用户名和密码,在以 'esendex' 为键的数组中。
config/services.php
return [ //other stuff 'esendex' => [ 'reference' => '', // your account reference 'username' => '', // your account username 'password' => '', // your account password 'default_from' => 'Laravel', // optional name of the sender 'dry_run' => false, // only for the notification channel, if true, no sms's will be sent 'throw_exception_on_error' => true // This will throw up the Esendex sdk exception if an exception is thrown by the dispatchService on the EsendexSmsChannel ]; ];
使用方法
您可以使用 facade 别名 Esendex 来执行 esendex sdk 的服务。认证参数将被自动注入。
Esendex::dispatchService()->send(new DispatchMessage( $sender , $phone , $text , Message::SmsType ));
您可以在 这个页面 上查看 esendex sdk 服务的完整列表。
通知通道
此包包含一个通知通道,允许您将 Esendex 服务与 Laravel 通知集成。
格式化通知
如果通知支持通过 Esendex 发送短信,您应该在通知类上定义 toEsendex 方法。此方法将接收一个 $notifiable 实体,并应返回一个 Jlorente\Laravel\Esendex\Notifications\Messages\EsendexMessage 实例或包含要发送的消息的字符串
/** * Get the Esendex / SMS representation of the notification. * * @param mixed $notifiable * @return \Jlorente\Laravel\Esendex\Notifications\Messages\EsendexMessage|string */ public function toEsendex($notifiable) { return (new EsendexMessage) ->content('Your SMS message content'); }
完成后,您必须将通知通道添加到通知的 via() 方法的数组中
/** * Get the notification channels. * * @param mixed $notifiable * @return array|string */ public function via($notifiable) { return [EsendexSmsChannel::class]; }
自定义发件人名称
如果您想用与在您的 config/services.php 文件中指定的不同的名称发送一些通知,您可以在 EsendexMessage 实例上使用 from 方法
/** * Get the Esendex / SMS representation of the notification. * * @param mixed $notifiable * @return \Jlorente\Laravel\Esendex\Notifications\Messages\EsendexMessage|string */ public function toEsendex($notifiable) { return (new EsendexMessage) ->content('Your SMS message content') ->from('Popilio'); }
路由通知
通过 esendex 通道发送通知时,通知系统将自动查找 notifiable 实体上的 phone_number 属性。如果您想自定义通知发送到的电话号码,请在实体上定义 routeNotificationForEsendex 方法
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable; /** * Route notifications for the Esendex channel. * * @param \Illuminate\Notifications\Notification $notification * @return string */ public function routeNotificationForEsendex($notification) { return $this->phone; } }
您可以在 这个页面 上找到有关 Laravel 通知的更多信息。
许可
版权 © 2018 José Lorente Martín jose.lorente.martin@gmail.com.
许可协议为 BSD 3-Clause License。详细信息请参阅 LICENSE.txt。