fowitech / laravel-sms
Laravel 的持久化短信包
dev-main
2024-03-03 15:10 UTC
Requires
- php: ^7.1 || ^8.0
Requires (Dev)
- phpunit/phpunit: ^7.0 || ^8.0
This package is auto-updated.
Last update: 2024-09-03 16:26:21 UTC
README
这是一个用于短信网关集成的 Laravel 包。现在发送短信变得简单。
支持网关列表
📦 安装
通过 Composer
$ composer require fowitech/laravel-sms
⚡ 配置
发布配置文件
$ php artisan vendor:publish --tag="sms"
在配置文件中,您可以设置所有短信的默认驱动程序。但您也可以在运行时更改驱动程序。
选择您希望为应用程序使用的网关。然后将其作为默认驱动程序,这样您就无需在所有地方指定它。但是,您也可以在一个项目中使用多个网关。
// Eg. if you want to use Netgsm. 'driver' => env('SMS_DRIVER', 'netgsm'),
然后在该驱动程序的数组中填写该网关的凭证。
// Eg. for Netgsm. 'netgsm' => [ 'transport' => \Fowitech\Sms\Drivers\Netgsm::class, 'sender' => env('NETGSM_SENDER', ''), 'username' => env('NETGSM_USERNAME', ''), 'password' => env('NETGSM_PASSWORD', ''), 'options' => [ // some options ] ],
🔥 使用方法
在您的代码中只需像这样使用即可。
# On the top of the file. use Sms; ... # In your Controller. Sms::message("this message")->to(['Number 1', 'Number 2'])->send(); # If you want to use a different driver. Sms::via('gateway')->message("this message")->to(['Number 1', 'Number 2'])->send(); # If you want to use options array $options = [ 'send_date' => now()->addHour() ]; Sms::via('gateway')->message("this message")->to(['Number 1', 'Number 2'])->send($options); # If you are not a Laravel's facade fan, you can use sms helper: sms()->message("this message")->to(['Number 1', 'Number 2'])->send(); # If you want to use a different driver. sms()->via('gateway')->message("this message")->to(['Number 1', 'Number 2'])->send();
😍 通道使用
首先,您必须使用 php artisan make:notification
命令创建您的通知,然后可以使用 SmsChannel::class
作为通道,如下所示
namespace App\Notifications; use Illuminate\Bus\Queueable; use Fowitech\Sms\Channels\SmsChannel; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; class InvoicePaid extends Notification { use Queueable; /** * Get the notification channels. * * @param mixed $notifiable * @return array|string */ public function via($notifiable) { return [SmsChannel::class]; } /** * Get the recipients and body of the notification. * * @param mixed $notifiable * @return Builder */ public function toSms($notifiable) { return "Sms message"; } }
自定义驱动程序,如何创建
首先,您必须在驱动程序数组中命名您的驱动程序,并且可以指定任何您想要的配置参数。
'my_driver' => [ 'transport' => \App\Packages\SMSDriver\MyDriver::class, 'sender' => env('MYDRIVER_SENDER', ''), 'username' => env('MYDRIVER_USERNAME', ''), 'password' => env('MYDRIVER_PASSWORD', ''), 'options' => [ // some options ] ... # Your Config Params here. ]
例如:您创建了一个类:App\Packages\SMSDriver\MyDriver
。
namespace App\Packages\SMSDriver; use Fowitech\Sms\Drivers\Driver; class MyDriver extends Driver { public function __construct($options = []) { $this->sender = config('sms.my_driver.sender'); $this->username = config('sms.my_driver.username'); $this->password = config('sms.my_driver.password'); $this->client = $this->getInstance(); } public function send($options = []) { # Main logic of Sending SMS. } }
🔬 测试
composer test