parsadanashvili/laravel-smsoffice

此包最新版本(v1.1.0)没有可用的许可证信息。

v1.1.0 2023-08-25 16:58 UTC

This package is auto-updated.

Last update: 2024-09-25 19:34:36 UTC


README

Laravel SMSOffice 包

Latest Stable Version Total Downloads Downloads Month

Laravel SMSOffice 包允许您通过 SMSOffice API 轻松发送短信。它提供了一个方便的方法将短信功能集成到您的 Laravel 应用程序中。

鸣谢

此包是从 lotuashvili/laravel-smsoffice 的原始工作中派生出来的。您可以在这里找到原始仓库。

安装

您可以使用 Composer 安装此包

composer require parsadanashvili/laravel-smsoffice

如果您使用的是 Laravel 5.4 或更低版本,您需要手动将服务提供者添加到您的 config/app.php 文件中。打开 config/app.php 文件,并将 SmsOfficeServiceProvider 添加到 providers 数组中

// config/app.php
'providers' => [
    // Other providers
    Parsadanashvili\LaravelSmsOffice\SmsOfficeServiceProvider::class,
],

添加服务提供者后,您需要发布配置

php artisan vendor:publish --provider="Parsadanashvili\LaravelSmsOffice\SmsOfficeServiceProvider"

配置

您需要在您的 .env 文件中设置以下环境变量

SMSOFFICE_API_KEY=your_api_key
SMSOFFICE_SENDER=your_default_sender
SMSOFFICE_DRIVER=your_preferred_driver

开发

在开发过程中,您可以通过将 SMSOFFICE_DRIVER=log 添加到您的 .env 文件中将 SMS 驱动程序设置为 log。这将通过记录操作而不是发送 SMS 来帮助您模拟 SMS 发送。

用法

使用通知

  1. 在您的 User 模型(或您想要发送通知的任何模型)中,添加一个返回用户电话号码的 routeNotificationForSms() 方法
namespace App\Models;

use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    // Other code...

    public function routeNotificationForSms()
    {
        return $this->phone; // Adjust this to match your phone number field
    }
}
  1. 使用以下 Artisan 命令创建通知类
php artisan make:notification SmsNotification
  1. 在生成的 SmsNotification 类中,导入 SmsOfficeChannel 并将其添加到 via() 方法中。定义 toSms() 方法以指定短信内容
use Illuminate\Notifications\Notification;
use Parsadanashvili\LaravelSmsOffice\SmsOfficeChannel;

class SMSNotification extends Notification
{
    public function via($notifiable)
    {
        return [SmsOfficeChannel::class];
    }

    public function toSms($notifiable)
    {
        return 'Your Notification Content Here';
    }
}
  1. 现在您可以在应用程序中使用此通知来向用户发送短信通知
use App\Notifications\SMSNotification;

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

直接发送 SMS

如果您想不使用通知直接发送 SMS 消息,您可以直接这样做

use Parsadanashvili\LaravelSmsOffice\SmsOffice;

public function sendSms(SmsOffice $smsOffice)
{
    $smsOffice->send('599123456', 'Your Message Here');
}

请随意将 '599123456' 替换为接收者的电话号码,并将 'Your Message Here' 替换为实际的消息内容。

这应该有助于阐明 Laravel SMSOffice 包的安装过程、配置和用法。